Python:从网址下载图片,但收到HTTP错误404

时间:2018-12-16 19:49:09

标签: python

我正在尝试从网站下载图像,但出现404错误。我试图添加一个没有成功的用户代理。

代码如下:

import requests
import shutil

with open(r'C:\Users\home\Desktop\urls.csv') as file:
    csv = []
    for row in file:
        csv.append(row.split(";"))


row = 0

while row < len(csv):
    r = requests.get(csv[row][0], stream=True, headers={'User-agent': 'Mozilla/5.0'})
    if r.status_code == 200:
        with open(r"C:\Users\home\Desktop\images\house" + str(row) + ".jpg", 'wb') as f:
            r.raw.decode_content = True
            shutil.copyfileobj(r.raw, f)
    row +=1

网址为:

https://example.com/wp-content/uploads/2018/10/toronto-curbed-8.jpg 用cdn.icepop替换示例

0 个答案:

没有答案