簡體   English   中英

BrowserMob Proxy Python - 如何獲取響應正文?

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

我需要使用 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()

您可以通過proxy.har['log']['entries']具有相同名稱的鍵來獲取請求和響應。 響應的內容在entry['response']['content']entry['response']['content']

但在您必須將'captureContent':True添加到proxy.new_har調用的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']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM