【问题标题】:Get the list of APIs called on loading of a URL programtically以编程方式获取加载 URL 时调用的 API 列表
【发布时间】:2022-01-22 21:49:16
【问题描述】:

当我们使用Ctrl + Shift + I 来检查网页的流量时,我们如何通过 python 检索网页加载时正在调用的 API,在“网络”部分下网页。

我尝试了请求模块,但我们只能检索页面 HTML,而不能检索网页调用的 API。 我还研究了 selenium web-driver 自动化,但没有运气。

import requests

resp = requests.get('https://edition.cnn.com/2021/12/21/australia/australia-new-south-wales-covid-omicron-intl-hnk/index.html')
html_data = resp.content

这里,html_data 仅包含页面 HTMl,但不包含作为页面加载的一部分加载的 API。

【问题讨论】:

    标签: python selenium selenium-webdriver web-scraping


    【解决方案1】:

    有一个基于 selenium 的实现,仅用于称为 selenium-wire。首先通过命令安装 selenium-wire

    pip install selenium-wire
    

    那么对于你的页面,你可以使用这个

    from seleniumwire import webdriver
    from selenium.webdriver.chrome.service import Service
    
    service = Service("D://drivers//chromedriver.exe")
    options = webdriver.ChromeOptions()
    
    driver = webdriver.Chrome(service=service, options=options)
    
    driver.get('https://edition.cnn.com/2021/12/21/australia/australia-new-south-wales-covid-omicron-intl-hnk/index.html')
    
    for request in driver.requests:
        if request.response:
            print(
                request.url,
                request.response.status_code,
                request.response.headers['Content-Type']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多