[Python] Selenium+browsermob-proxy截取har內容

先配置java運行環境,然後下載browsermob-proxy

https://github.com/lightbody/browsermob-proxy/releases

安裝browsermob-proxy

pip3 install browsermob-proxy
from browsermobproxy import Server
server = Server("path of the browsermob-proxy")
#/bin/browsermob-proxy.bat
server.start()
proxy = server.create_proxy()
chrome_options = Options()
chrome_options.add_argument('--proxy-server={0}'.format(proxy.proxy))
chrome_options.add_argument('ignore-certificate-errors')

browser = webdriver.Chrome(options=chrome_options)
base_url = 'https://google.com'
proxy.new_har("new_har", options={'captureContent': True})

browser.get(base_url)
result = proxy.har
for entry in result['log']['entries']:
    print(entry['response']['content'])