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

Download ans save many PDFs files with python

我正在尝试从网站下载许多 PDFS 文件并保存它们。

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:\Users\king-\OneDrive\Bureau\pdfs\pdfs.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 是一个 built-in 函数(在 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)