Selenium Wire简单介绍
Selenium Wire扩展了Selenium的Python绑定,使您能够访问浏览器发出的底层请求。您以与使用Selenium相同的方式编写代码,但是您获得了额外的api,用于检查请求和响应,并动态地对它们进行更改。
工作原理
Selenium Wire的工作原理是将浏览器流量重定向到它在后台运行的内部代理服务器。当请求流经代理服务器时,它们被拦截和捕获。捕获请求可能会使事情变慢,但你可以做一些事情来限制被捕获的内容。
安装
pip install selenium-wire
官方举例
from seleniumwire import webdriver # Import from seleniumwire
# Create a new instance of the Chrome driver
driver = webdriver.Chrome()
# 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']
)
Prints:
https://www.google.com/ 200 text/html; charset=UTF-8
https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png 200 image/png
https://consent.google.com/status?continue=https://www.google.com&pc=s×tamp=1531511954&gl=GB 204 text/html; charset=utf-8
https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png 200 image/png
https://ssl.gstatic.com/gb/images/i2_2ec824b0.png 200 image/png
https://www.google.com/gen_204?s=webaft&t=aft&atyp=csi&ei=kgRJW7DBONKTlwTK77wQ&rt=wsrt.366,aft.58,prt.58 204 text/html; charset=UTF-8