代码
from browsermobproxy import Server from selenium import webdriver import os from urllib import parse from time import sleep server = Server(r'/Users/renguanyu/app/browsermob-proxy/2.1.4/bin/browsermob-proxy') server.start() proxy = server.create_proxy() chromedriver = "/usr/local/bin/chromedriver" os.environ["webdriver.chrome.driver"] = chromedriver url = parse.urlparse (proxy.proxy).path chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--ignore-certificate-errors') chrome_options.add_argument("--proxy-server={0}".format(url)) driver = webdriver.Chrome(chromedriver,chrome_options =chrome_options) driver.implicitly_wait(60) proxy.new_har("http://localhost:8082/", options={'captureHeaders': True,'captureContent': True}) driver.get("http://localhost:8082/") sleep(3) # 打印network result = proxy.har log = result["log"] entries = log["entries"] list = [] for entrie in entries: request = entrie["request"] request_url = request["url"] response = entrie["response"] status = response["status"] dict = { "url": request_url, "status": status } list.append(dict) # sleep(30) print("request_list") for item in list: print(item) print("request_list_length", len(list)) proxy.close() driver.quit()
解决了吗?