8

我需要使用 Selenium Chrome 驱动程序和 browsermob 代理获取 POST 请求的响应正文内容。目前,当我阅读它时,此内容不包含在我的文件 HAR 输出中,尽管我可以在浏览器网络流量中看到响应。我怎样才能让它捕获响应流量?(很抱歉编程新手,看不到太多 BMP 的 python 文档)

    server.start()
    代理 = server.create_proxy()
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument("--proxy-server={0}".format(proxy.proxy))
    驱动程序 = webdriver.Chrome(executable_path="chromedriver.exe", chrome_options=chrome_options)

    proxy.new_har("req", options={'captureHeaders': True,'captureContent':True})
    driver.get('https://www.example.com/something')


    result_har = json.dumps(proxy.har, ensure_ascii=False)
    使用 open("haroutput.har", "w") 作为 harfile:
        harfile.write(result_har)

    server.stop()
    driver.quit()

4

1 回答 1

5

您可以在proxy.har['log']['entries']. 响应的内容在entry['response']['content']

但在你必须添加'captureContent':True到调用的option字典之前proxy.new_har

例子:

from browsermobproxy import Server

server = Server("./bin/browsermob-proxy")

server.start()
proxy = server.create_proxy()

from selenium import webdriver
co = webdriver.ChromeOptions()
co.add_argument('--proxy-server={host}:{port}'.format(host='localhost', port=proxy.port))

driver = webdriver.Chrome(executable_path="chromedriver", chrome_options=co)

proxy.new_har('req',options={'captureHeaders': True,'captureContent':True})

driver.get(url)
proxy.har  # returns a HAR

for ent in proxy.har['log']['entries']:
    _url = ent['request']['url']
    _response = ent['response']
    _content = _response['content']['text']
于 2018-07-12T03:15:18.277 回答