【问题标题】:How i can download image from multiple url in a list我如何从列表中的多个 url 下载图像
【发布时间】:2020-10-13 17:09:28
【问题描述】:

如何从 pandas 数据框中的列表中的多个 url 下载所有图像。我是python新手。

# for a single url, i can download the image:

url = ['http://pbs.twimg.com/media/EPFVkqcWkAYTDVi.jpg'] 

import wget
for photos in url:
    wget.download(photos)

# But i have multiple url in the list. 

url = ['http://pbs.twimg.com/media/EPFVkqcWkAYTDVi.jpg,http://pbs.twimg.com/media/EPFVkqcWkAYTDVi.jpg,http://pbs.twimg.com/media/EPFVkqcWkAYTDVi.jpg'] 

# i get error trying to download all the images
**Exception: 404: Not Found**

# I also want the download to skip any invalid url and continue.

【问题讨论】:

  • 您必须将字符串拆分为列表。 url = url[0].split(',')
  • 单独引用每个 URL,使每个 URL 都是一个字符串。

标签: python-3.x pandas url jupyter-notebook wget


【解决方案1】:

分别引用每个 url 并使用 Try\Except 以防出错:

试试这个代码:

url = ['https://images.wallpaperscraft.com/image/pier_sea_horizon_189419_168x300.jpg',
       'https://images.wallpaperscraft.com/image/paint_liquid_fluid_art_189413_168x300.jpg',
       'https://images.wallpaperscraft.com/image/sunset_glare_city_189428_168x300.jpg',
       'https://images.wallpaperscraft.com/image/maple_leaf_macro_189405_168x300.jpg']
        
import wget
import time  # for sleep

for photos in url:
    try: 
       print('\nGet:', photos)   
       wget.download(photos)
       time.sleep(1)  # pause 1 second, if needed
    except Exception as ex:
       print('Failed to get:',photos, ex)

输出

Get: https://images.wallpaperscraft.com/image/pier_sea_horizon_189419_168x300.jpg
100% [................................................................................] 7216 / 7216
Get: https://images.wallpaperscraft.com/image/paint_liquid_fluid_art_189413_168x300.jpg
100% [..............................................................................] 10146 / 10146
Get: https://images.wallpaperscraft.com/image/sunset_glare_city_189428_168x300.jpg
100% [................................................................................] 7412 / 7412
Get: https://images.wallpaperscraft.com/image/maple_leaf_macro_189405_168x300.jpg
100% [..............................................................................] 12837 / 12837

【讨论】:

  • @Mike64,代码似乎有效,异常​​也有效。但是代码只下载了三个图像中的两个。如何修改它以下载所有三个图像?即使我使用了四个网址,也只下载了两张图片。谢谢
  • 我用 4 张图片更新了答案。他们都正确下载。如果下载有问题,可以在循环中添加sleep或者添加逻辑重试下载,
  • 是的,它在循环中下载了所有内容。谢谢
猜你喜欢
  • 1970-01-01
  • 2015-06-15
  • 1970-01-01
  • 1970-01-01
  • 2019-05-19
  • 2012-12-15
  • 2019-12-24
  • 2021-08-12
  • 2014-09-07
相关资源
最近更新 更多