我使用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'
答案 0 :(得分:0)
我不建议使用selenium来完成这项任务。如果您有网址列表,只需使用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作为第一个参数传递,将目标作为最终参数传递。
答案 1 :(得分:0)
感谢大家的帮助.Below是我正在使用的代码,它的工作正常。
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是一个列表,我必须迭代并将其添加到我需要下载的网页中,因此您可以对其进行修改。