首先用 pipe 安装
pip install selenium pip install browsermob-proxy
然后在下载编译好的执行包 ,在bin/ 目录下有browsermob-proxy 和 browsermob-proxy.bat 俩个文件,分别是用于linux和windows。
from browsermobproxy import Serverimport json# 此处的路径即为上述可执行文件的所在的路径server = Server("/home/zyk/Downloads/browsermob-proxy-2.1.1/bin/browsermob-proxy")server.start()proxy = server.create_proxy()from selenium import webdriverprofile = webdriver.FirefoxProfile()profile.set_proxy(proxy.selenium_proxy())driver = webdriver.Firefox(firefox_profile=profile)proxy.new_har("baidu")driver.get("http://www.baidu.com")# proxy.wait_for_traffic_to_stop(1, 60)#保存为指定的 .har 文件with open('1.har', 'w') as outfile: json.dump(proxy.har, outfile)server.stop()driver.quit()
程序执行完成以后,得到的 1.har 文件可以使用 HTTP Archive Viewer 这个chrome插件来查看。