浏览器mob代理Python如何获取响应体?

2024-10-01 11:21:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要使用Selenium Chrome驱动程序和browsermob代理获取POST请求的响应正文内容。目前,当我阅读该内容时,该内容未包含在我的文件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()


Tags: true内容serverdriverselenium驱动程序resultchrome
1条回答
网友
1楼 · 发布于 2024-10-01 11:21:01
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']

您可以在中获取请求和响应代理.har['log']['entries']

相关问题 更多 >