【发布时间】:2019-11-06 14:24:04
【问题描述】:
我为循环中大量下载 PDF 文件编写了一个脚本。基于 Firefox 的 Selenium-Webdriver 在第一次下载后卡住了,所以我决定尝试 Chrome。
现在有了 Chrome,Loop 就可以工作了。但我无法下载 PDF 文件。 Chrome 只是在浏览器窗口中使用 pdf_viewer.js 显示它们。
我尝试了不同的选项,例如 plugins.plugins_list": [{"enabled": False, "name": "Chrome PDF Viewer"}]、download.extensions_to_open": "applications/pdf 和 plugins.always_open_pdf_externally": True。没有任何效果。
#!/usr/bin/python
downPath = "/home/user/xxx/"
Y = ["2017", "2018", "2019"]
def main():
options = webdriver.ChromeOptions()
profile = {
"plugins.plugins_list": [{"enabled": False, "name": "Chrome PDF Viewer"}],
"download.default_directory": downPath ,
"download.extensions_to_open": "applications/pdf",
"plugins.always_open_pdf_externally": True,
"download.prompt_for_download": False,
"safebrowsing.enabled": True
}
options.add_experimental_option("prefs", profile)
browser = webdriver.Chrome(options=options)
driver = webdriver.Chrome()
driver.get('login-url')
#LOGIN-Stuff ...
time.sleep(4)
for y in Y:
print(y)
for x in range(53):
url = "https://j.world/files/{0}/filename_{0}-{1:02d}.pdf".format(y, x)
print(url)
driver.get(url)
time.sleep(4)
driver.quit()
if __name__ == "__main__":
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
import sys
import os
main()
我使用 Python 3.7.4 和来自 Arch-Linux 存储库的 python-selenium 3.141.0-1。
【问题讨论】: