【问题标题】:is it possible to get the selenium headless chrome XHR response when using Python 3使用 Python 3 时是否可以获得 selenium headless chrome XHR 响应
【发布时间】:2021-12-05 06:20:14
【问题描述】:

我正在使用 selenium google chrome headless 来捕获一些内容,我试图做的是解析网页源代码并获取我想要的数据列表。我必须编写一些解析 html 和 css 代码来获得我想要的内容。代码如下:

    @staticmethod
    def fetch_music_download_url_impl(music_name: str):
        chrome_driver_service = Service(ChromeDriverManager(chrome_type=ChromeType.GOOGLE).install())
        chrome_options = Options()
        chrome_options.add_argument("--no-sandbox")
        chrome_options.add_argument("--headless")
        chrome_options.add_argument("--disable-gpu")
        chrome_options.add_argument("--remote-debugging-port=9230")
        driver = webdriver.Chrome(service=chrome_driver_service,
                                  options=chrome_options,
                                  executable_path="/usr/local/bin/chromedriver")
        try:
            driver.maximize_window()
            driver.get('http://example.cn/music/?page=audioPage&type=migu&name=' + music_name)
            driver.implicitly_wait(5)
            driver.find_element(By.CSS_SELECTOR, ".aplayer-list-download.iconfont.icon-xiazai").click()
            urls = [a.get_attribute('href') for a in
                    driver.execute_script('return document.querySelectorAll(".modal-body a[href*=\'http\']")')]
            for url in urls:
                if "listenSong.do" in url:
                    logger.info("fetched url:" + url)
                    FetchMusic.do_save_music_download_url(url)
        except Exception as e:
            logger.error("scrapy impl error", e)
        finally:
            driver.stop_client()
            driver.close()
            driver.quit()
            chrome_driver_service.stop()

这段代码有效,这只是一个演示,如果我想让它更完美,我必须编写更多的代码来匹配数据。我想知道是否可以使用我想要获得的 json 内容来获得 google chrome 无头 XHR 响应内容。所以我可以做更简单的步骤,只需解析 json 响应。这样会更简单可靠,节省资源。

我应该怎么做才能获得谷歌浏览器无头 XHR json 响应?

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    我正在使用selenium-wire 这样做:

    from seleniumwire import webdriver
    

    然后使用此代码:

        @staticmethod
        def fetch_search_xhr_response(driver: any):
            for request in driver.requests:
                if request.response and r'api/search' in request.url:
                    if request.response.status_code == http.client.OK:
                        search_response = request.response.body
                        json_response = json.loads(search_response)
                        if json_response["code"] == http.client.OK:
                            FetchMusic.handle_music_list(json_response["data"])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-19
      • 2019-05-08
      • 2018-01-10
      • 1970-01-01
      • 2023-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多