如何使用请求下载图像我正在尝试用python的requests模块。下面是我使用的(工作)代码:img = urllib2.urlopen(settings.STATICMAP_URL.format(**data))with open(path, 'w') as f:
f.write(img.read())下面是使用requests:r = requests.get(settings.STATICMAP_URL.format(**data))if r.status_code == 200:
img = r.raw.read()
with open(path, 'w') as f:
f.write(img)您能帮助我了解响应中要使用的哪些属性吗?requests?
3 回答
繁星点点滴滴
TA贡献1803条经验 获得超3个赞
response.raw
response.rawdecode_contentTrue (requestsFalseshutil.copyfileobj()
import requestsimport shutil r = 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)
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)
Response.iter_content()
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)
stream=Truerequests
添加回答
举报
0/150
提交
取消
