【发布时间】:2016-08-13 04:20:06
【问题描述】:
我想使用 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
硒必须有类似的方法吗?
提前致谢
更新:
用 browsermob-proxy 解决了。
pip3 install browsermob-proxy
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']]
【问题讨论】:
-
除了用
pip安装python库外,还需要从https://github.com/lightbody/browsermob-proxy/releases下载最新版本的bmp并安装java运行环境apt-get install default-jre.<path to browsermob-proxy>然后设置为您将 bmp 下载到的路径。
标签: python python-3.x selenium phantomjs