如何使用 selenium 和 Python 迭代和下载多个 pdf

How to iterate and download multiple pdfs using selenium and Python

我对使用 selenium 有点陌生,Python.Below 是我尝试 运行 下载多个文件的代码。

from selenium import webdriver
driver = webdriver.Chrome(executable_path=r'C:\chromedriver_win32\chromedriver.exe')
cusip=['abc123','def456','ghi789']
for a in cusip:

    page=driver.get("http://mylink=" + str(a) + ".pdf")
    with open(a + '.pdf', 'wb') as f:
        for chunk in page.iter_content(chunk_size=1024):
            if chunk:
                f.write(chunk)

我收到的错误如下:

Traceback (most recent call last):
  File "C:/Users/shashi.singh/PycharmProjects/HiSSS/Selenium.py", line 13, in <module>
    for chunk in page.iter_content(chunk_size=1024):
AttributeError: 'NoneType' object has no attribute 'iter_content'

我不建议为此任务使用硒。如果您有 url 的列表,只需使用 urllib.request.urlretrive:

In [5]: from urllib import request

In [6]: request.urlretrieve('https://arxiv.org/pdf/1409.8470.pdf', r'C:\users\chris\test.pdf')
Out[6]: ('C:\users\chris\test.pdf', <http.client.HTTPMessage at 0x59628d0>)

只需将每个 url 作为第一个参数传递,并将目的地作为最后一个参数传递。

感谢大家的帮助。下面是我正在使用的代码,它运行良好。

from selenium import webdriver
driver = webdriver.Chrome(executable_path=r'C:\chromedriver_win32\chromedriver.exe')
cusip=['abc123','def456','ghi789']
options = webdriver.ChromeOptions()

tgt = "C:\directory"  #target directory to download item
profile = {"plugins.plugins_list": [{"enabled":False, "name":"Chrome PDF Viewer"}],
    "download.default_directory" : tgt}
options.add_experimental_option("prefs",profile)
print(options)
driver = webdriver.Chrome(executable_path=r'C:\chromedriver_win32\chromedriver.exe', chrome_options=options)

for a in cusip:
    page=driver.get("http://mylink=" + str(a) + ".pdf") #iterate the item in cusip list

Print('Process completed Successfully')

cusip 是一个列表,我必须对其进行迭代并将其添加到我需要下载的网页中,因此您可以根据需要对其进行修改。