Downloading Images with Requests
Downloading images from the web using Python's requests module can be a straightforward task. However, when switching from urllib2 to requests, it's crucial to understand the differences in the attributes used to retrieve the image data.
Initially, the code using urllib2 read the image using img.read(), while the desired approach with requests has img = r.raw.read(). This is erroneous as r.raw is a file object, not the actual image data.
To resolve this, there are two viable solutions:
Using the Response.raw File Object:
Set decode_content to True to force decompression of compressed responses. This allows you to stream the data to a file object using shutil.copyfileobj().
import requests import shutil r = requests.get(url, 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:
This method ensures data decompression.
r = requests.get(url, stream=True) if r.status_code == 200: with open(path, 'wb') as f: for chunk in r: f.write(chunk)
Alternatively, you can specify a custom chunk size using Response.iter_content().
r = requests.get(url, 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 ('wb') to prevent newlines from being translated. Additionally, set stream=True to prevent the entire image from being downloaded into memory.
The above is the detailed content of How to Efficiently Download Images Using Python's Requests Library?. For more information, please follow other related articles on the PHP Chinese website!