gpt4 book ai didi

python - BrowserMob Proxy Python - 如何获取响应主体?

转载 作者:太空狗 更新时间:2023-10-30 01:25:33 42 4
gpt4 key购买 nike

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

    server.start()    proxy = server.create_proxy()    chrome_options = webdriver.ChromeOptions()    chrome_options.add_argument("--proxy-server={0}".format(proxy.proxy))     driver = 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)    with open("haroutput.har", "w") as harfile:        harfile.write(result_har)    server.stop()    driver.quit()

最佳答案

您可以通过 proxy.har['log']['entries'] 中具有相同名称的 key 获取请求和响应。响应的内容在 entry['response']['content']

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

例子:

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']

关于python - BrowserMob Proxy Python - 如何获取响应主体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50550496/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com