Downloading Images Using Requests Module in Python
In an effort to download and save an image from the web using the requests module, a developer encounters difficulties. The code provided initially works, but the modified code using requests results in an error.
The error arises due to the use of an incorrect attribute from the requests response. To successfully retrieve the image using requests, there are two options:
Using the response.raw File Object
Utilizing the response.raw attribute returns the raw data of the response. Decoding compressed responses (e.g., using GZIP or deflate) is not handled automatically. To force decompression, set the decode_content attribute to True. Subsequently, use shutil.copyfileobj() to stream the data to a file object.
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)
Iterating Over the Response
An alternative approach involves iterating over the response, which ensures data decompression.
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)
Customizing the chunk size is possible using the Response.iter_content() method.
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)
Remember to open the destination file in binary mode to prevent newline translation and set stream=True to avoid memory-intensive full downloads.
The above is the detailed content of How to Efficiently Download Images with Python's Requests Module?. For more information, please follow other related articles on the PHP Chinese website!