徐汇网站制作,东莞做网站 南城信科,大数据培训班需要多少钱,WordPress搭建连不上数据库目录 前言
seleniumwire简介
功能
兼容性
目录
安装
创建webdriver
获取请求
请求对象
限制请求捕获 前言
有时候需要知道UI界面操作的同时接口响应数据是否正常#xff0c;这时就需要获取接口响应数据。Selenium本身没有获取接口响应的api#xff0c;但是可以通过…目录 前言
seleniumwire简介
功能
兼容性
目录
安装
创建webdriver
获取请求
请求对象
限制请求捕获 前言
有时候需要知道UI界面操作的同时接口响应数据是否正常这时就需要获取接口响应数据。Selenium本身没有获取接口响应的api但是可以通过第三方库seleniumwire获取接口响应数据。
seleniumwire简介
SeleniumWire扩展了Selenium的Python绑定使您可以访问浏览器发出的底层请求。您以与Selenium相同的方式编写代码但您获得了额外的API来检查请求和响应并对其进行动态更改。
功能
纯Python用户友好的API捕获HTTP和HTTPS请求拦截请求和响应动态修改标题、参数和正文内容捕获websocket消息支持HAR格式代理服务器支持
兼容性
Python 3.7Selenium 4.0.0Chrome, Firefox, Edge and Remote Webdriver supported
目录
安装
pip install selenium-wire创建webdriver
from seleniumwire import webdriver注意不是从selenium包中导入。
然后像直接使用Selenium一样实例化web驱动程序。您可以传入任何所需的功能或特定于浏览器的选项如可执行路径、无头模式等。seleniumwire也有自己的选项可以在seleniumwire_options属性中传递。
# Create the driver with no options (use defaults)
driver webdriver.Chrome()# Or create using browser specific options and/or seleniumwire_options options
driver webdriver.Chrome(options webdriver.ChromeOptions(...),seleniumwire_options{...}
)
请注意对于webdriver的子包您应该继续直接从selenium导入这些子包。例如要导入WebDriverWait
# Sub-packages of webdriver must still be imported from selenium itself
from selenium.webdriver.support.ui import WebDriverWait
获取请求
SeleniumWire捕获浏览器发出的所有HTTP/HTTPS流量[1]。以下属性提供对请求和响应的访问权限。
driver.requests
按时间顺序捕获的请求的列表。
driver.last_request
用于检索最近捕获的请求的便利属性。这比使用driver.requests[-1]更有效。
请求对象
body
以字节为单位的请求正文。如果请求没有正文则正文的值将为空即b。
headers
请求头的类似字典的对象。标头不区分大小写允许重复。请求.headers[user-agent]将返回用户代理标头的值。如果你想替换一个标头请确保先用del request.headers〔header-name〕删除现有的标头否则你会创建一个重复的标头。
response
与请求关联的响应对象。如果请求没有响应则此选项将为“无”。
限制请求捕获
SeleniumWire的工作原理是通过后台启动的内部代理服务器重定向浏览器流量。当请求流经代理时它们会被拦截和捕获。捕获请求可能会稍微减慢速度但您可以做一些事情来限制捕获的内容。 driver.scopes 这接受一个正则表达式列表这些正则表达式将与要捕获的URL相匹配。在提出任何请求之前应该在驱动程序上设置它。当为空默认值时将捕获所有URL。
driver.scopes [.*stackoverflow.*,.*github.*
]driver.get(...) # Start making requests# Only request URLs containing stackoverflow or github will now be captured
seleniumwire_options.exclude_hosts
排除捕获域名不需要捕获的可以加入排除选项
使用此选项可以完全绕过Selenium Wire。对此处列出的地址发出的任何请求都将直接从浏览器发送到服务器而不涉及SeleniumWire。请注意如果您已经配置了上游代理那么这些请求也将绕过该代理。
options {exclude_hosts: [host1.com, host2.com] # Bypass Selenium Wire for these hosts
}
driver webdriver.Chrome(seleniumwire_optionsoptions)
测试代码
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],request.response.body)
输出
https://www.google.com/ 200 text/html; charsetUTF-8
https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png 200 image/png
https://consent.google.com/status?continuehttps://www.google.compcstimestamp1531511954glGB 204 text/html; charsetutf-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?swebafttaftatypcsieikgRJW7DBONKTlwTK77wQrtwsrt.366,aft.58,prt.58 204 text/html; charsetUTF-8
...
其他功能可以参考
https://github.com/wkeeling/selenium-wire