Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用selenium捕获网络流量_Python_Django_Python 3.x_Selenium Webdriver - Fatal编程技术网

Python 如何使用selenium捕获网络流量

Python 如何使用selenium捕获网络流量,python,django,python-3.x,selenium-webdriver,Python,Django,Python 3.x,Selenium Webdriver,我正在启动一个新的Django项目,我正在尝试用Selenium捕获网络流量 我已经使用Selenium wire(MITM代理)实现了这个目标,但是Django不喜欢使用Selenium wire(必须使用--nothreading--noreload“连接错误…”启动服务器)。 我正在寻找现代解决方案来实现这一点,比如直接解析firefox的网络开发工具,或者使用firefox插件。 我正在使用FirefoxGeckodriver进行测试 范围(0,10)内x的: profile=profi

我正在启动一个新的Django项目,我正在尝试用Selenium捕获网络流量

我已经使用Selenium wire(MITM代理)实现了这个目标,但是Django不喜欢使用Selenium wire(必须使用--nothreading--noreload“连接错误…”启动服务器)。 我正在寻找现代解决方案来实现这一点,比如直接解析firefox的网络开发工具,或者使用firefox插件。 我正在使用FirefoxGeckodriver进行测试

范围(0,10)内x的
:
profile=profile_firefox()
options=options\u firefox()
driver=webdriver.Firefox(Firefox_profile=profile,options=options,executable_path='/Users/*****/Desktop/selenium master/headless_browser/geckodriver')
尝试:
驱动程序。设置车窗位置(0,0)
驱动程序。设置窗口大小(randint(10242060),randint(10244100))
睡眠时间(randint(3,10))
获取驱动程序(url)
wait=ui.WebDriverWait(驱动程序,10)
睡眠时间(randint(8,10))
if driver.find_element_by_xpath(“/*[@id=\“container\”]/main/div/div/div[1]/div[2]/form/div/div[2]/div[1]/button”):
驱动程序。通过xpath(“//*[@id=\“container\”]/main/div/div[1]/div[2]/form/div/div[2]/div[1]/button”)查找元素。单击()
del driver.requests
睡眠时间(randint(8,10))
驱动程序。通过xpath(“//*[@id=\“container\”]/main/div/div[1]/div[2]/form/div/div[2]/div[1]/button”)查找元素。单击()
睡眠时间(randint(10,20))
对于driver.requests中的请求:
如果request.path==”https://api.*********.**/*******/*******":
请求\ api=请求
raw=str(请求\ api.body)
请求_api=原始分割(('b\''))
有效负载\u原始=请求\u api[1]
有效载荷=有效载荷_原始[:-1]
如果有效载荷:
header=request.headers
时间。睡眠(8)
打破
除:
打印(“休斯顿关于欧盟-联合国问题”)
firefox\u关闭(驱动程序)
编辑:


def profile_firefox():
profile=FirefoxProfile()
profile.set_首选项('permissions.default.image',2)
profile.set_首选项('dom.ipc.plugins.enabled.libflashplayer.so','false')
profile.set_首选项(“general.useragent.override”,firefox_init())
profile.set_首选项('network.proxy.type',1)
profile.set_首选项('network.proxy.socks','localhost')
profile.set_首选项('network.proxy.socks_port',9050)
profile.set_首选项(“network.proxy.socks_remote_dns”,False)
profile.set_首选项(“driver.privatebrowsing.autostart”,True)
profile.update_preferences()
返回配置文件
使用Socks、HTTP、SSL配置的测试2:


server=server('/Users/*****/Desktop/selenium master/browsermob-proxy-2.1.4/bin/browsermob proxy')
server.start()
proxy=server.create_proxy()
proxy.selenium_proxy()#不明白它是做什么的???
port=int(proxy.port)
profile=FirefoxProfile()
profile.set_首选项('permissions.default.image',2)
profile.set_首选项('dom.ipc.plugins.enabled.libflashplayer.so','false')
profile.set_首选项('general.useragent.override',firefox_init())
profile.set_首选项('network.proxy.type',1)
profile.set_首选项('network.proxy.socks','localhost')
profile.set_首选项('network.proxy.socks_port',9050)
profile.set_首选项('network.proxy.ssl','localhost')
profile.set_首选项('network.proxy.ssl_port',port)
profile.set_首选项('network.proxy.http','localhost')
profile.set_首选项('network.proxy.http_port',port)
profile.set_首选项('network.proxy.socks_remote_dns',False)
profile.set_首选项('driver.privatebrowsing.autostart',True)
profile.update_preferences()
Http代理似乎覆盖了socks配置


如果您对我的代码或解决方案有任何线索或建议,非常感谢。

您可以使用代理来捕获网络流量。在Python中与selenium配合良好。您需要先下载browsermob可执行文件。这是Firefox的一段代码:

from browsermobproxy import Server
from selenium import webdriver

server = Server('path_to_executable')
server.start()
proxy = server.create_proxy()
profile  = webdriver.FirefoxProfile()
profile.set_proxy(proxy.selenium_proxy())
driver = webdriver.Firefox(firefox_profile=profile)
proxy.new_har("file_name", options={'captureHeaders': True, 'captureContent': True})
driver.get("your_url")
proxy.wait_for_traffic_to_stop(1, 60)
for ent in proxy.har['log']['entries']:
    print(ent)

server.stop()
driver.quit()

Browsermob是正确的方法

我必须了解browsermob是如何工作的,还有tor。 对于Tor,您必须像这样启用HTTPTunnelPort配置

tor --HTTPTunnelPort 8088
并配置browsermob以使用它

proxy_params = {'httpProxy': 'localhost:8088', 'httpsProxy': 'localhost:8088'}
proxy_b = server.create_proxy(params=proxy_params)

谢谢。

Python有一个名为selenium wire的包。您可以使用该软件包捕获网络流量并对其进行验证。selenium wire是selenium的扩展版本,它将使用selenium的所有功能以及额外的API来捕获网络并进行验证。下面是一篇文章的链接

下面是包的存储库

示例代码-

from seleniumwire import webdriver  # Import from seleniumwire

# Create a new instance of the Firefox driver
driver = webdriver.Firefox()

# Go to the Google home page
driver.get('https://www.google.com')

# Access requests via the `requests` attribute
for request in driver.requests:
    if request.response:
        print(
            request.url,
            request.response.status_code,
            request.response.headers['Content-Type']
        )

谢谢你的回答。我已经编辑了我的第一篇文章,在我的Firefox配置文件中,我正在使用Tor通过9050端口旋转我的IP。是否可以将您的解决方案与我的配置相结合?我不知道它是否兼容,我从未使用过Tor。可能在代理之间会有一些端口/主机重定向。我被卡住了,当我在配置文件配置中手动设置socks、http、ssl代理时,socks代理无法工作。。。。如果配置文件配置无法在浏览器上运行,我正在寻找类似的解决方案