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
相关推荐
aqi0019 小时前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵20 小时前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf21 小时前
Agent 流程编排
后端·python·agent
copyer_xyf1 天前
Agent RAG
后端·python·agent
copyer_xyf1 天前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf1 天前
Agent 记忆管理
后端·python·agent
星云穿梭2 天前
用Python写一个带图形界面的学生管理系统——完整教程
python
金銀銅鐵2 天前
用 Pygame 实现 15 puzzle
python·数学·游戏
黄忠2 天前
大模型之LangGraph技术体系
python·llm