1

我正在为browsermob 代理使用 python包装器,我需要修改服务器响应。但我不知道我该怎么做。谁能给我一个工作代码示例?

文档说我可以为它发送一个 javascript 代码,但它对我不起作用:

server = Server(path=SCRIPT_DIR+"/browsermob-proxy/bin/browsermob-proxy", options={"port": 8888})
server.start()
proxy = server.create_proxy()

proxy.response_interceptor('contents.setTextContents("<html><body>Response successfully intercepted</body></html>")')

fp = webdriver.FirefoxProfile()
browser = webdriver.Firefox(firefox_profile=fp, proxy=proxy)
browser.get("http://example.com")

谢谢!

4

2 回答 2

0

在 browsermobproxy 0.7.1 版中,response_interceptor 存在错误。你可以去github获取最新的代码,或者你可以使用这个变通方法而不是 response_interceptor:

import requests

url = proxy.host + '/proxy/' + str(proxy.port) + '/filter/response'
headers={'Content-Type':'text/plain'}

filter = "contents.setTextContents('<html><body>Response successfully intercepted</body></html>');"

r = requests.post(url=url, data=filter, headers=headers)
于 2016-10-13T07:59:00.807 回答
0

像魅力一样工作 - 只是不要忘记在您的 Firefox 配置文件上设置代理;) fp.set_proxy(proxy.selenium_proxy())

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium import webdriver
from browsermobproxy import Server
from pprint import pprint
from time import sleep
import requests


server = Server($PATH_GOES_HERE$)
server.start()


proxy = server.create_proxy()

proxy.new_har('test111')

url = proxy.host + '/proxy/' + str(proxy.port) + '/filter/response'
headers={'Content-Type':'text/plain'}

filter = "response.setTextContents('<html><body>Response successfully intercepted</body></html>');"

r = requests.post(url=url, data=filter, headers=headers)





fp = webdriver.FirefoxProfile()
fp.set_proxy(proxy.selenium_proxy()) # <---- you forgot this one here ;)
browser = webdriver.Firefox(firefox_profile=fp, proxy=proxy)
sleep(3)
browser.get("example.com")
于 2018-01-09T20:41:24.980 回答