Pyhon使用requests、urllib下载图片的几种方法
通过urllib将图片下载到当前目录内,命名test.png
from urllib.request import urlretrieve
IMAGE_URL = "https://viencoding.com/images/vultr.png"
urlretrieve(IMAGE_URL, './test.png')
通过requests将图片下载到当前目录内,命名test2.png
import requests
IMAGE_URL = "https://viencoding.com/images/vultr.png"
resp = requests.get(IMAGE_URL)
with open('./test2.png', 'wb') as f:
f.write(resp.content)
通过requests将图片分块下载到当前目录内,命名test3.png
import requests
IMAGE_URL = "https://viencoding.com/images/vultr.png"
resp = requests.get(IMAGE_URL, stream=True)
with open('./test3.png', 'wb') as f:
for chunk in resp.iter_content(chunk_size=32):
f.write(chunk)
viencoding.com版权所有,允许转载,但转载请注明出处和原文链接: https://viencoding.com/article/183