########################
with open('output.jpg', 'wb') as handle:
response = requests.get('
http://www.example.com/image.jpg', stream=True)
if not response.ok:
# Something went wrong
for block in response.iter_content(1024):
if not block:
break
handle.write(block)
########################
/*I usually just use urllib.urlretrieve(). It works, but if you need to use a session or some sort of authentication, the above code works as well.*/
voir aussi :
https://stackoverflow.com/questions/13137817/how-to-download-image-using-requests
-----------------------------------------------------------------------------------------------------------------------------------------
Get a file-like object from the request and copy it to a file. This will also avoid reading the whole thing into memory at once.
########################
import shutil
import requests
url = '
http://example.com/img.png'
response = requests.get(url, stream=True)
with open('img.png', 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
del response
########################
Note that the .raw file-like object does not decompress a response using Content-Encoding: gzip or Content-Encoding: deflate. You can use response.raw.read(decode_content=True) but shutil.copyfileobj cannot do that because it assumes a generic .read() method
########################
import requests
import 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)
########################
ou :
#######################
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)
########################