首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用python/selenium/机器人框架获取所有XHR的头数据

如何使用python/selenium/机器人框架获取所有XHR的头数据
EN

Stack Overflow用户
提问于 2022-03-07 12:24:47
回答 2查看 336关注 0票数 0

我需要从网页中获取API的所有请求URL,同时浏览它,有人能帮我吗?

其目的是比较调用和假定调用的API列表。

我尝试使用driver.get_log(“性能”) selenium方法,但我只获得.jpgs和png文件

代码语言:javascript
运行
复制
driver.get("https:<URL>")

logs = driver.get_log("performance")
# Opens a writable JSON file and writes the logs in it
with open("network_log.json", "w", encoding="utf-8") as f:
    f.write("[")

    # Iterates every logs and parses it using JSON
    for log in logs:
        network_log = json.loads(log["message"])["message"]
        f.write(json.dumps(network_log)+",")
EN

回答 2

Stack Overflow用户

发布于 2022-03-07 14:42:50

你不能。硒不能处理那种事。您将需要使用一个带有selenium的代理,比如浏览器或其他允许您拦截所有网络请求的代理。

票数 0
EN

Stack Overflow用户

发布于 2022-10-07 23:10:09

这有点晚了,但我最近遇到了类似的情况,通过为我的chrome驱动程序设置这个配置,我能够获得所有的XHR:

代码语言:javascript
运行
复制
from selenium import webdriver
from selenium.webdriver import DesiredCapabilities
import json
import os

chromedriver_path = f'{os.getcwd()}//chromedriver.exe'

chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_argument("--disable-single-click-autofill")
chrome_options.add_argument("--disable-autofill-keyboard-accessory-view[8]")
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)
chrome_options.add_experimental_option("prefs", prefs)
capabilities = DesiredCapabilities.CHROME
capabilities['goog:loggingPrefs'] = {"performance": 'ALL'}

driver_obj = webdriver.Chrome(chromedriver_path, chrome_options=chrome_options, desired_capabilities=capabilities)

logs = [json.loads(log["message"])["message"] for log in driver_obj.get_log("performance")]

希望它能帮到别人!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71381044

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档