python--requests下载图片
来源:互联网 发布:周立功单片机使用方法 编辑:程序博客网 时间:2024/05/16 01:56
转载自http://stackoverflow.com/questions/13137817/how-to-download-image-using-requests
You can either use the response.raw
file object, or iterate over the response.
To use the response.raw
file-like object will not, by default, decode compressed responses (with GZIP or deflate). You can force it to decompress for you anyway by setting the decode_content
attribute to True
(requests
sets it to False
to control decoding itself). You can then use shutil.copyfileobj()
to have Python stream the data to a file object:
import requestsimport shutilr = requests.get(settings.STATICMAP_URL.format(**data), stream=True)if r.status_code == 200: with open(path, 'wb') as f: r.raw.decode_content = True shutil.copyfileobj(r.raw, f)
To iterate over the response use a loop; iterating like this ensures that data is decompressed by this stage:
r = requests.get(settings.STATICMAP_URL.format(**data), stream=True)if r.status_code == 200: with open(path, 'wb') as f: for chunk in r: f.write(chunk)
This'll read the data in 128 byte chunks; if you feel another chunk size works better, use the Response.iter_content()
method with a custom chunk size:
r = requests.get(settings.STATICMAP_URL.format(**data), stream=True)if r.status_code == 200: with open(path, 'wb') as f: for chunk in r.iter_content(1024): f.write(chunk)
Note that you need to open the destination file in binary mode to ensure python doesn't try and translate newlines for you. We also set stream=True
so that requests
doesn't download the whole image into memory first.
- python--requests下载图片
- requests 下载图片
- python requests 断点续传下载
- python requests 下载安装
- python requests post 文件,图片
- python爬虫(豆瓣影评requests版并下载电影封面图片)
- 使用requests模块下载爬虫百度图片
- python requests.get() 下载大文件
- python Requests 知乎问题图片爬虫
- python+requests+bs4 爬取暴走GIF图片
- python +requests 实现爬取百度图片
- 利用Python实现基于Requests框架和多线程技术的Tumblr博客空间图片及视频下载
- Python上传磁盘和网络图片,内存图片,使用requests
- requests库入门-13-如何下载一个图片资源
- python requests
- python requests
- Python Requests
- python Requests
- ceph 故障解决备忘
- 静态嵌套类(Static Nested Class)和内部类(Inner Class)的不同
- Linux中vi使用
- Android系统onKeyDown事件
- jsp中获取get请求过来的参数
- python--requests下载图片
- try ... catch 与 __try ... __except
- 下拉ScrollView伸缩头布局,实现ScrollView回弹效果
- 网络异常那些事
- MFC程序宽字节和UTF互转
- 北斗波束
- ssh key login
- Ugly Number whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not
- Hadoop IO 特性详解(2)【文件校验】