【记录】selenium爬取chorme网页pdf时download.default directory与download.prompt_for_download失效的原因

项目场景:

有时候我们要爬取的数据在网站中是以pdf形式存储的,传统的基于xpath元素定位的方式无法定位数据,因此我们需要获取pdf文件内容再进一步分析;

有时候也可能我们是单纯需要把网站的pdf爬下来存起来;

实际上这种场景更适合使用requests库,因为速度更快,奇奇怪怪的问题更少,但是selenium具有可视化、反爬概率相对低、上手更简单的特质,所以学习怎么用selenium来爬网站pdf也是有必要的。

这篇文章记录一次selenium+choremedriver来实现自动化爬pdf时遇到的bug以及解决办法。

注:强烈推荐大家阅读这篇推文,有selenium爬pdf的详细方法与设置:
【记录】Python3|Selenium 下载 PDF 不预览不弹窗(2024年)


bug记录:

在设置Chrome浏览器的选项和偏好设置时,编码如下:

options = Options()
options.add_argument('log-level=3')
options.add_argument("--incognito")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_experimental_option('useAutomationExtension', False)
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_argument("--disable-blink-features=AutomationControlled")

prefs = {
    'profile.default_content_settings.popups': 0,
     'download.default_directory': r"D:\\PDF",
    "download.prompt_for_download": False,
    "download.directory_upgrade": True,
    "plugins.always_open_pdf_externally": True,
}
options.add_experimental_option('prefs', prefs)

上述代码中,
"download.prompt_for_download": False这个选项的设置是禁止Chrome浏览器在下载文件时弹出询问框。
'download.default_directory'则是指定你下载的pdf存储位置。
两者都是为了实现自动化爬pdf

但运行代码下载url中的pdf时,依旧弹窗询问文件下载位置,并且默认位置没有变到D盘PDF文件夹:

在这里插入图片描述

bug原因:

经排查,是对chorme浏览器时那一堆配置中的语句**options.add_argument(“–incognito”)**与我们的"download.prompt_for_download"和’download.default_directory’是相冲突的。

根据文档:List of Chromium Command Line Switches,对于–incognito的解释为:Forces Incognito mode even if user data directory is specified using the --user-data-dir switch.(即使用户通过使用"–user-data-dir"开关指定了用户数据目录,也会强制启用隐身模式(Incognito mode))

即,无论用户是否指定了用户数据目录,Chrome浏览器都会强制以隐身模式启动。这意味着无论用户之前是否启用了隐身模式、是否已指定了用户数据目录,Chrome都会以隐身模式运行,并且不会将用户的浏览历史、Cookie、缓存等数据保存到磁盘上。确保用户数据不会被保留或需要临时隔离用户数据的测试环境。通过强制启用隐身模式,可以确保用户的隐私和数据安全。

所以出现download.default directory与download.prompt_for_download失效的原因是:隐身模式下使用的是隐身默认的配置,故我们对于浏览器选项的修改失效了。

解决办法:

删掉隐身模式的配置(因为本身这次爬虫已经使用了代理,相对安全)即可实现pdf的自动化下载了
注:此外原代码还有个错误,即: ‘download.default_directory’: r"D:\PDF",应该是 r"D:\PDF",因为r的意思是raw字符串,所以不需要转义字符,也就不需要\(我感觉在python里面最好还是用/ 吧)

本人比较菜,如果有不对的地方希望大佬指出,谢谢!

  • 43
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你在 CentOS 7 上使用 Selenium 和 Chrome,且设置 `download.default_directory` 不生效,可能是因为 Chrome 在 headless 模式下没有权限访问文件系统。你可以尝试以下解决方案: 1. 给 Chrome 添加用户组权限 执行以下命令,将当前用户添加到 `chrome` 用户组中: ``` sudo usermod -a -G chrome $USER ``` 然后重新启动 Chrome,试试能否下载文件。 2. 设置 Chrome 的命令行参数 在启动 Chrome ,通过命令行参数来设置 `download.default_directory`。例如: ```python from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument('--disable-dev-shm-usage') options.add_argument('--no-sandbox') options.add_argument('--headless') options.add_argument('--disable-gpu') options.add_argument('--disable-browser-side-navigation') options.add_argument('--disable-infobars') options.add_argument('--disable-popup-blocking') options.add_argument('--start-maximized') options.add_argument('--window-size=1920,1080') options.add_argument('--disable-setuid-sandbox') options.add_argument('--disable-extensions') options.add_argument('--disable-features=VizDisplayCompositor') options.add_argument('--disable-features=NetworkService') options.add_argument('--disable-features=VizHitTestSurfaceLayer') options.add_argument('--disable-features=TaskSchedulerFromIdle') options.add_argument('--disable-features=RendererCodeIntegrity') options.add_argument('--disable-features=AudioServiceOutOfProcess') options.add_argument('--disable-features=VizDisplayCompositor') options.add_argument('--disable-features=OutOfBlinkCors') options.add_argument('--disable-features=RendererCodeIntegrityVerification') options.add_argument('--disable-features=RendererCodeIntegrity') options.add_argument('--disable-features=AudioServiceOutOfProcess') options.add_argument('--disable-features=VizHitTestSurfaceLayer') options.add_argument('--disable-features=VizHitTestDrawQuad') options.add_argument('--disable-features=VizHitTestDrawQuad') options.add_argument('--disable-features=NetworkService') options.add_argument('--disable-features=TaskSchedulerFromIdle') options.add_argument('--disable-features=OutOfBlinkCors') options.add_argument('--disable-features=RendererCodeIntegrityVerification') options.add_argument('--disable-features=RendererCodeIntegrity') options.add_argument('--disable-features=NetworkServiceInProcess') options.add_argument('--disable-features=VizDisplayCompositor') options.add_argument('--disable-features=VizHitTestSurfaceLayer') options.add_argument('--disable-features=VizHitTestDrawQuad') options.add_argument('--disable-features=VizHitTestDrawQuad') options.add_argument('--disable-features=TaskSchedulerFromIdle') options.add_argument('--disable-features=RendererCodeIntegrityVerification') options.add_argument('--disable-features=RendererCodeIntegrity') options.add_argument('--disable-features=NetworkService') options.add_argument('--disable-features=OutOfBlinkCors') options.add_argument('--disable-features=VizHitTestSurfaceLayer') options.add_argument('--disable-features=NetworkServiceInProcess') options.add_argument('--disable-features=VizDisplayCompositor') options.add_argument('--disable-features=VizHitTestDrawQuad') options.add_argument('--disable-features=RendererCodeIntegrityVerification') options.add_argument('--disable-features=RendererCodeIntegrity') options.add_argument('--disable-features=VizHitTestDrawQuad') options.add_argument('--disable-features=NetworkService') options.add_argument('--disable-features=TaskSchedulerFromIdle') options.add_argument('--disable-features=OutOfBlinkCors') options.add_argument('--disable-features=RendererCodeIntegrityVerification') options.add_argument('--disable-features=RendererCodeIntegrity') options.add_argument('--disable-features=AudioServiceOutOfProcess') options.add_argument('--disable-features=VizDisplayCompositor') options.add_argument('--disable-features=VizHitTestSurfaceLayer') options.add_argument('--disable-features=NetworkServiceInProcess') options.add_argument('--disable-features=VizHitTestDrawQuad') options.add_argument('--disable-features=VizHitTestDrawQuad') options.add_argument('--disable-features=RendererCodeIntegrityVerification') options.add_argument('--disable-features=RendererCodeIntegrity') options.add_argument('--disable-features=NetworkService') options.add_argument('--disable-features=TaskSchedulerFromIdle') options.add_argument('--disable-features=OutOfBlinkCors') options.add_argument('--disable-features=VizHitTestSurfaceLayer') options.add_argument('--disable-features=RendererCodeIntegrityVerification') options.add_argument('--disable-features=RendererCodeIntegrity') options.add_argument('--disable-features=VizHitTestDrawQuad') options.add_argument('--disable-features=NetworkService') options.add_argument('--disable-features=TaskSchedulerFromIdle') options.add_argument('--disable-features=OutOfBlinkCors') options.add_argument('--disable-features=VizHitTestSurfaceLayer') options.add_argument('--disable-features=RendererCodeIntegrityVerification') options.add_argument('--disable-features=RendererCodeIntegrity') options.add_argument('--disable-features=VizHitTestDrawQuad') options.add_argument('download.default_directory=/path/to/download/directory') driver = webdriver.Chrome(options=options) ``` 其中,`/path/to/download/directory` 为你要下载文件的目录路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值