做国外网站用什么颜色,制作网页时科学规划网页的做法是,门户网站建设费,鑫三科技网站设计本文使用的版本#xff1a; Chrome 124Python 12Selenium 4.19.0 版本过旧可能会出现问题#xff0c;但只要别差异太大#xff0c;就可以看本文#xff0c;因为本文对新老版本都有讲解。 文章目录 1 难点解析和具体思路2 注意事项2.1 PDF 资源获取时注意事项2.2 Capabiliti… 本文使用的版本 Chrome 124Python 12Selenium 4.19.0 版本过旧可能会出现问题但只要别差异太大就可以看本文因为本文对新老版本都有讲解。 文章目录 1 难点解析和具体思路2 注意事项2.1 PDF 资源获取时注意事项2.2 Capabilities 写法2.3 get_log(performance) 写法 3 完整代码 1 难点解析和具体思路
这个难点主要是 Chrome 和 Selenium 的版本更新太快了。
首先如果要继承 Selenium 的 Headers有两种思路
从 Selenium 对于 Chromedriver的参数入手即 arguments[0]这样的东西。参考示例代码如下# Execute JavaScript to retrieve headers
headers driver.execute_script(var headersObj {};var headers new Map(Object.entries(arguments[0].headers));headers.forEach(function(value, key) {headersObj[key] value;});return headersObj;
, driver.execute_script(return window.navigator))具体driver是什么我也不解释了总之就是这个其实就是个人工配置项arguments[0]里根本就不会自带一个headers键值。arguments里面可能存在的所有参数可以看这篇文章List of Chromium Command Line Switcheshttps://peter.sh/experiments/chromium-command-line-switches/。从 Selenium 抓的包入手即使用 network 相关的在 Selenium 里面是 get_log(performance)。这个方式在 Selenium 4.10 之后有所改变具体改变见下文。
2 注意事项
我这篇文章需要继承 headers 是因为网络上有些资源是需要登录注册的但是每次都自己重新获取 Cookie 是很麻烦的。我这里以一个随便找的 PDF 资源https://www.sigmaaldrich.cn/CN/zh/sds/aldrich/488488的获取为例。
2.1 PDF 资源获取时注意事项
具体可以看【记录】PythonSelenium 下载 PDF 不预览不弹窗2024年代码的解释也写了这部分就不展开说了本文的最后面贴了完整的代码。
2.2 Capabilities 写法 参考How to Capture Network Traffic When Scraping with Selenium Python 在 Chrome 75 之后这部分出现了改变。Chrome 和 chromedriver 的版本很重要。版本 75 左右的日志记录功能发生了变化以适应 W3C 合规性。如果您卡在 Chrome/chromedriver 版本 75 以下则需要在下面的第一个代码片段中使用loggingPrefs而不是goog:loggingPrefs。
caps DesiredCapabilities.CHROME
# capabilities[loggingPrefs] {performance: ALL} # chromedriver ~75
caps[goog:loggingPrefs] {performance: ALL}2.3 get_log(“performance”) 写法 参考Getting TypeError: WebDriver.init() got an unexpected keyword argument ‘desired_capabilities’ when using Appium with Selenium 4.10-Stackoverflow 在 Selenium 4.10 之后这部分出现了改变。
Selenium 4.10 之前
driver webdriver.Chrome(services, optionsoptions, desired_capabilitiescaps) # selenium 4.10Selenium 4.10 之后
options.set_capability(goog:loggingPrefs, {performance: ALL})
driver webdriver.Chrome(services, optionsoptions)3 完整代码
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.common.desired_capabilities import DesiredCapabilitiescaps DesiredCapabilities.CHROME
# capabilities[loggingPrefs] {performance: ALL} # chromedriver ~75
caps[goog:loggingPrefs] {performance: ALL}options Options()
# options.add_argument(
# user-agentMozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36) # UA
# options.add_argument(user-data-dirC:/Users/User/AppData/Local/Google/Chrome/User Data/Default)
s Service(D:/software/chromedriver.exe)
# Disable the built-in PDF viewer
options.add_experimental_option(prefs, {download.prompt_for_download: True,plugins.always_open_pdf_externally: False})
# desired_capabilities has been removed according to this post,so the newest way looks like this : options webdriver.ChromeOptions() options.set_capability(goog:loggingPrefs, {performance: ALL})
# driver webdriver.Chrome(services, optionsoptions, desired_capabilitiescaps) # selenium 4.10
options.set_capability(goog:loggingPrefs, {performance: ALL})
driver webdriver.Chrome(services, optionsoptions)pdf_url https://www.sigmaaldrich.cn/CN/zh/sds/aldrich/488488# get driver log
driver.get(pdf_url)
print(driver.log_types)
network_logs driver.get_log(performance)import json
# Extract headers from the network logs
headers {}
for log in network_logs:log_message json.loads(log[message])[message] # Parse log message as JSONif params in log_message and request in log_message[params]:request_params log_message[params][request]if headers in request_params:headers request_params[headers]break # Exit loop after finding headersimport requests# Use requests to download the PDF file with headers
response requests.get(pdf_url, headersheaders)# Check if the request was successful
if response.status_code 200:# Save the PDF filewith open(output.pdf, wb) as f:f.write(response.content)print(PDF file downloaded successfully.)
else:print(Failed to download the PDF file.)# Close the Selenium WebDriver
driver.quit()这样子写代码就不需要 Selenium 去 sleep 等待下载了也可以很好地解决一部分 Requests 库的反爬虫问题不过对于防止重放攻击的反爬虫手段还是无效。 本账号所有文章均为原创欢迎转载请注明文章出处https://blog.csdn.net/qq_46106285/article/details/137891147。百度和各类采集站皆不可信搜索请谨慎鉴别。技术类文章一般都有时效性本人习惯不定期对自己的博文进行修正和更新因此请访问出处以查看本文的最新版本。