python下载图片的几种方法
一. urllib
# python2
import urllib url = r"https://storage.googleapis.com/imgfave/image_cache/1491751695365459.jpg" path = r"D:\my.jpg" #字符串前加r 防止被转义 data = urllib.urlretrieve(url,path) #py -3 #data = urllib.request.urlretrive(url,path)
#python3 import urllib.request url = r"https://storage.googleapis.com/imgfave/image_cache/1491751695365459.jpg" path = r"D:\my.jpg" #字符串前加r 防止被转义 data = urllib.request.urlretrieve(url,path)
二. requests
import requests url = 'https://storage.googleapis.com/imgfave/image_cache/1491751695365459.jpg' with open(r'D:\sss.jpg', 'wb') as handle: response = requests.get(url, stream=True) for block in response.iter_content(1024): if not block: break handle.write(block) print('done')
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步