如何从多个URL下载图像?

时间:2019-04-04 02:37:34

标签: python-3.x python-requests

我正在尝试从多个链接下载图像,我尝试使用下面的代码,这不会引发任何错误,但是只有最后一张图像正在下载。

这是我的源代码:

import requests

image_url = [
  "https://vision-images-store.s3.amazonaws.com/internship/zipper/view_1/3213e9e9da734c268db6bed4b76ea411.jpg",
  "https://vision-images-store.s3.amazonaws.com/internship/zipper/view_1/3a4e9c2704bb46afb5a43c3231974e04.jpg",
  "https://vision-images-store.s3.amazonaws.com/internship/zipper/view_1/48d93cb06ede452fbab83495a4ff17a6.jpg",
  "https://vision-images-store.s3.amazonaws.com/internship/zipper/view_1/c7d91a112c6341eb84ed8e62ea4d6aa8.jpg",
  "https://vision-images-store.s3.amazonaws.com/internship/zipper/view_1/5cb6f2cdd3a244af9d9640be6e65b4f0.jpg",
  "https://vision-images-store.s3.amazonaws.com/internship/zipper/view_1/b5804b834f2945dabd87892df695f5b2.jpg",
  "https://vision-images-store.s3.amazonaws.com/internship/zipper/view_1/c1a05e6fa0b24a2490b10b80760dae83.jpg"
]

for img in image_url:
  file_name = img.split('/')[-1]
  print("Downloading file:%s"%file_name)
  r = requests.get(img, stream = True)
  with open("file_name", 'wb') as f:
    for chunk in r:
      f.write(chunk)

我在做什么错了?

2 个答案:

答案 0 :(得分:1)

您的代码无法正常工作,因为您使用的是字符串"file_name",而不是变量,请尝试以下操作:

import requests

image_url = [
  "https://vision-images-store.s3.amazonaws.com/internship/zipper/view_1/3213e9e9da734c268db6bed4b76ea411.jpg",
  "https://vision-images-store.s3.amazonaws.com/internship/zipper/view_1/3a4e9c2704bb46afb5a43c3231974e04.jpg",
  "https://vision-images-store.s3.amazonaws.com/internship/zipper/view_1/48d93cb06ede452fbab83495a4ff17a6.jpg",
  "https://vision-images-store.s3.amazonaws.com/internship/zipper/view_1/c7d91a112c6341eb84ed8e62ea4d6aa8.jpg",
  "https://vision-images-store.s3.amazonaws.com/internship/zipper/view_1/5cb6f2cdd3a244af9d9640be6e65b4f0.jpg",
  "https://vision-images-store.s3.amazonaws.com/internship/zipper/view_1/b5804b834f2945dabd87892df695f5b2.jpg",
  "https://vision-images-store.s3.amazonaws.com/internship/zipper/view_1/c1a05e6fa0b24a2490b10b80760dae83.jpg"
]

for img in image_url:
 file_name = img.split('/')[-1]
 print("Downloading file:%s"%file_name)
 r = requests.get(img, stream=True)
 # this should be file_name variable instead of "file_name" string
 with open(file_name, 'wb') as f:
    for chunk in r:
       f.write(chunk)

答案 1 :(得分:0)

this project中,我采取了类似的方法,从网站本身获取的URL列表中,从网站下载大量图像。

enter image description here