小编Bar*_*art的帖子

使用Selenium/PhantomJS进行网络捕获

我想使用Selenium和python捕获我正在浏览的网站的流量,因为使用代理的流量将是https不会让我走得太远.

我的想法是运行带有selenium的phantomJS并使用phantomJS来执行脚本(不是在页面上使用webdriver.execute_script(),而是在phantomJS本身上).我在考虑netlog.js脚本(来自https://github.com/ariya/phantomjs/blob/master/examples/netlog.js).

因为它在命令行中这样工作

phantomjs --cookies-file=/tmp/foo netlog.js https://google.com
Run Code Online (Sandbox Code Playgroud)

用硒必须有类似的方法吗?

提前致谢

更新:

用browsermob-proxy解决了它.

pip3 install browsermob-proxy
Run Code Online (Sandbox Code Playgroud)

Python3代码

from selenium import webdriver
from browsermobproxy import Server

server = Server(<path to browsermob-proxy>)
server.start()
proxy = server.create_proxy({'captureHeaders': True, 'captureContent': True, 'captureBinaryContent': True})

service_args = ["--proxy=%s" % proxy.proxy, '--ignore-ssl-errors=yes']
driver = webdriver.PhantomJS(service_args=service_args)

proxy.new_har()
driver.get('https://google.com')
print(proxy.har)  # this is the archive
# for example:
all_requests = [entry['request']['url'] for entry in proxy.har['log']['entries']]
Run Code Online (Sandbox Code Playgroud)

python selenium python-3.x phantomjs

5
推荐指数
2
解决办法
7048
查看次数

标签 统计

phantomjs ×1

python ×1

python-3.x ×1

selenium ×1