简体   繁体   English

BrowserMob Proxy Python - 如何获取响应正文?

[英]BrowserMob Proxy Python - How to get response body?

I need to get the response body content for a POST request using Selenium Chrome driver and browsermob proxy.我需要使用 Selenium Chrome 驱动程序和 browsermob 代理获取 POST 请求的响应正文内容。 Currently this content is not included in my file HAR output when i read it although i can see the response in the browser network traffic.目前,虽然我可以在浏览器网络流量中看到响应,但当我阅读它时,此内容未包含在我的文件 HAR 输出中。 How can i make it so response traffic is captured?我怎样才能做到这样才能捕获响应流量? (sorry new to programming and can't see much python documentation for BMP) (对不起,编程新手,看不到很多 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()

You can get request and response by key with the identical name in proxy.har['log']['entries'] .您可以通过proxy.har['log']['entries']具有相同名称的键来获取请求和响应。 Response's content is under entry['response']['content']响应的内容在entry['response']['content']entry['response']['content']

But before you have to add 'captureContent':True into the option dict of proxy.new_har call.但在您必须将'captureContent':True添加到proxy.new_har调用的option字典proxy.new_har

Example:例子:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM