使用python下载并保存许多PDF文件



我正试图从网站下载许多PDFS fle并保存它们。

import requests
url = "https://jawdah.qcc.abudhabi.ae/en/Registration/QCCServices/Services/Registration/Trade%20Licenses/"+id+".pdf"
r = requests.get(url, stream= TRUE)
for id in range(1,125):
with open(id+'.pdf',"wb") as pdf:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
pdf.write(chunk)

pdf的第一个url是https://jawdah.qcc.abudhabi.ae/en/Registration/QCCServices/Services/Registration/Trade%20Licenses/1.pdf

最后一个url是https://jawdah.qcc.abudhabi.ae/en/Registration/QCCServices/Services/Registration/Trade%20Licenses/125.pdf

我想下载所有这些文件。当我执行这个代码时,我有这个错误

Traceback (most recent call last):
File "c:Usersking-OneDriveBureaupdfspdfs.py", line 6, in <module>
url = "https://jawdah.qcc.abudhabi.ae/en/Registration/QCCServices/Services/Registration/Trade%20Licenses/"+id+".pdf"
TypeError: can only concatenate str (not "builtin_function_or_method") to str

在第二行

url = "https://jawdah.qcc.abudhabi.ae/en/Registration/QCCServices/Services/Registration/Trade%20Licenses/"+id+".pdf"

str对象添加到名为id的对象中。id是一个内置函数(在python控制台中键入id()(。第4行

for id in range(1,125):

用其他东西(数字(覆盖id,这是可能的,但不推荐。

除此之外,您只需提出一个请求,而不是针对每个文件的请求。试试这个:

import requests
url = "https://jawdah.qcc.abudhabi.ae/en/Registration/QCCServices/Services/Registration/Trade%20Licenses/{}.pdf"
for num in range(1,126):
r = requests.get(url.format(num), stream= TRUE)
with open('{}.pdf'.format(num),"wb") as pdf:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
pdf.write(chunk)

最新更新