Python如何下载图片

2019-08-27 02:14:58   Python

Pyhon使用requests、urllib下载图片的几种方法

urllib download

通过urllib将图片下载到当前目录内,命名test.png

from urllib.request import urlretrieve

IMAGE_URL = "https://viencoding.com/images/vultr.png"
urlretrieve(IMAGE_URL, './test.png') 

requests download

通过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 chunk download

通过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)
vien.tech版权所有,允许转载,但转载请注明出处和原文链接: https://viencoding.com/article/183
欢迎小伙伴们在下方评论区留言 ~ O(∩_∩)O
文章对我有帮助, 点此请博主吃包辣条 ~ O(∩_∩)O

猜你喜欢


评论

There are no comments yet.
未登录

登录后即可发表评论

登录或注册

亲情非友情链接