我需要使用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()
答案 0 :(得分:2)
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']
您可以在proxy.har ['log'] ['entries']
中获得请求和响应