我需要从网页中获取API的所有请求URL,同时浏览它,有人能帮我吗?
其目的是比较调用和假定调用的API列表。
我尝试使用driver.get_log(“性能”) selenium方法,但我只获得.jpgs和png文件
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)+",")
发布于 2022-03-07 14:42:50
你不能。硒不能处理那种事。您将需要使用一个带有selenium的代理,比如浏览器或其他允许您拦截所有网络请求的代理。
发布于 2022-10-07 23:10:09
这有点晚了,但我最近遇到了类似的情况,通过为我的chrome驱动程序设置这个配置,我能够获得所有的XHR:
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")]
希望它能帮到别人!
https://stackoverflow.com/questions/71381044
复制相似问题