使用requests下载图片

Posted by Tesla9527 on October 16, 2020

转载至:原文地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import requests
import shutil


image_url = "https://cdn.pixabay.com/photo/2020/02/06/09/39/summer-4823612_960_720.jpg"
filename = image_url.split("/")[-1]
r = requests.get(image_url, stream = True)
if r.status_code == 200:
    r.raw.decode_content = True
    with open(filename,'wb') as f:
        shutil.copyfileobj(r.raw, f)
    print('Image sucessfully Downloaded: ',filename)
else:
    print('Image Couldn\'t be retreived')