您可以尝试以下代码
from browsermobproxy import Serverimport psutilimport timefor proc in psutil.process_iter(): # check whether the process name matches if proc.name() == "browsermob-proxy": proc.kill()dict = {'port': 8090}server = Server(path="./BrowserMobProxy/bin/browsermob-proxy", options=dict)server.start()time.sleep(1)proxy = server.create_proxy()time.sleep(1)from selenium import webdriverprofile = webdriver.FirefoxProfile()selenium_proxy = proxy.selenium_proxy()profile.set_proxy(selenium_proxy)driver = webdriver.Firefox(firefox_profile=profile)proxy.new_har("google")driver.get("http://www.google.co.uk")print (proxy.har) # returns a HAR JSON blobserver.stop()driver.quit()
两件事,如果您的代码失败,则有时可能会打开该过程。所以我在下面添加了相同的内容
import psutilimport timefor proc in psutil.process_iter(): # check whether the process name matches if proc.name() == "browsermob-proxy": proc.kill()
在创建代理之前和之后也要休眠1秒
server.start()time.sleep(1)proxy = server.create_proxy()time.sleep(1)
这有助于消除一些间歇性问题,这些问题在服务器启动之前可能会面临。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)