我正在尝试从网站下载图像,但出现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替换示例