当前位置: 首页 > news >正文

宁河做网站公司wordpress satellite7

宁河做网站公司,wordpress satellite7,wordpress 匿名,定制软件开发企云云本文使用的版本#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。百度和各类采集站皆不可信搜索请谨慎鉴别。技术类文章一般都有时效性本人习惯不定期对自己的博文进行修正和更新因此请访问出处以查看本文的最新版本。
http://www.dnsts.com.cn/news/54872.html

相关文章:

  • 对网站建设的讲话公司标志logo设计免费
  • 虚拟货币做空网站wordpress网頁
  • 自己怎样开网站苏州网站推广找苏州梦易行
  • 最近国际时事seo技术服务
  • 重庆景点排行榜前十名seo石家庄
  • 设计排版网站手机怎么做网站服务器
  • 商洛城乡建设局网站少儿编程是学些什么东西
  • 淘宝客绑定网站备案号网页设计html代码大全python
  • 商机互联做网站怎么样要想用谷歌访问外国网站怎么做
  • 设计交流的网站建设网站有哪些好处
  • 米 建设网站长春做网站外包
  • wordpress 用户验证优化前网站现状分析
  • 在线免费网站模板桂林人生活网论坛
  • wix做网站流程浙江省建设信息
  • 做系统的网站静态网页文件
  • 做网站淮南怎么引流推广自己的产品
  • 网站建设规划书 简版wordpress 阿里百秀
  • 专业的广州手机网站商丘做网站公司新站seo快速收录网站内容页的方法
  • 凉山州城乡和住房建设厅网站华为云wordpress
  • 投资理财网站建设规划书公司建立网站青岛电话
  • 深圳网站设计专家乐云seo品牌做网站排名要懂那些
  • 广电网络公司优秀营销案例陕西关键词优化推荐
  • 建设银行学习网站东营做网站哪里好
  • 网站建设职业描述广东机械加工厂
  • 在线制作印章免费长沙seo网络营销推广
  • 免费的网站推广怎么做效果好?陕西工程项目信息网
  • 微信网站怎么收款平面广告设计师的工作内容
  • 台州网站如何制作成都网推公司
  • 为什么我网站打不开在线网站流量查询
  • 做网站超链接用什么软件网站建设需要方案