seleniumwire获取页面接口数据

selenium并不支持获取响应的数据,我们可以使用selenium-wire库,selenium-wire扩展了 Selenium 的 Python 绑定,可以访问浏览器发出的底层请求。 编写的代码与 Selenium 的方式相同。

1. 先安装seleniumwire的插件

python 复制代码
pip install selenium-wire -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

注意:seleniumwire只兼容Selenium 4.0.0+,所以如果版本不兼容,请升级selenium

2. 在初始化浏览器的时候,引用的包由selenium改为seleniumwire

python 复制代码
# from selenium import webdriver
from seleniumwire import webdriver

3. 获取网络响应的内容,也就是如图部分的url响应:

python 复制代码
driver.requests

4. driver.requests返回的是一个列表,遍历每个url来获取想要的url响应信息。

python 复制代码
for request in driver.requests:
    if request.response:
        # 判断列表数据的接口在不在获取的请求中
        if '/sugrec' in request.url:
            print(
                # 请求方式
                request.method,
                # 获取请求参数
                request.body,
                # 获取请求头
                request.headers,
                # 获取结果的状态码
                request.response.status_code,
                # 获取请求的返回值
                request.response.body
            )
            break
相关推荐
chushiyunen1 天前
python chatTts实现tts文本转语音、音频
python
FreakStudio1 天前
把 Flask 搬进 ESP32,高中生自研嵌入式 Web 框架 MicroFlask !
python·单片机·嵌入式·cortex-m3·异步编程·电子diy
love530love1 天前
OpenClaw 手机直连配置全流程
人工智能·windows·python·智能手机·c#·agent·openclaw
chushiyunen1 天前
python中的内置属性 todo
开发语言·javascript·python
2301_793804691 天前
Python数据库操作:SQLAlchemy ORM指南
jvm·数据库·python
Hommy881 天前
【开源剪映小助手】IPC 通信机制
python·开源·aigc·剪映小助手
Zhansiqi1 天前
dayy43
pytorch·python·深度学习
紫丁香1 天前
pytest_自动化测试3
开发语言·python·功能测试·单元测试·集成测试·pytest
杰杰7981 天前
Python面向对象——类的魔法方法
开发语言·python
chushiyunen1 天前
python中的魔术方法(双下划线)
前端·javascript·python