如何在selenium python中从网络获取javascript和css请求的状态

2024-10-03 11:17:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我想获取我的网站https://lifesciences.cactusglobal.com/的特定javascript和css网络请求的状态。这些网络请求可以在chrome网络中看到。我想打印请求url及其响应代码。下面附上的屏幕截图仅供参考,请检查带下划线的请求:

enter image description here

我尝试使用包selenium-wire

driver.get('https://www.google.com')

for request in driver.requests:
    if request.response:
        print(
            request.url,
            request.response.status_code,
            request.response.headers['Content-Type']
        )

但它只返回输入的url的响应和标题,而不返回网络请求。在selenium python中,还有其他方法可以遍历网络请求吗?

谢谢


Tags: https网络comurl网站responserequest状态
2条回答

非常感谢你的支持。我找到了一个非常符合我要求的解决方案。
参考这些链接:
https://gist.github.com/rengler33/f8b9d3f26a518c08a414f6f86109863chttps://www.rkengler.com/how-to-capture-network-traffic-when-scraping-with-selenium-and-python/
还发现一个包是一个简单的解决方法(但不要坚持)-pip install selenium-wire

输出很混乱,但是尝试像这样获取网络请求,它似乎对我有用:

driver.get('https://lifesciences.cactusglobal.com/')
time.sleep(3)
test = driver.execute_script("var performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || {}; var network = performance.getEntries() || {}; return network;")

for item in test:
  print(item)

相关问题 更多 >