Urllib to download a file - consider
Download Image from URL using Python
Recently, I want to download some images using Python. This is what I’ve learned after survey.
The native and naive way is to use module to download an image.
However, the above code may error out with following message:
www.cronistalascolonias.com.arror: HTTP Error Forbidden
In this case, we need to add a HTTP header to the request:
A better way is to use requests package. Here is a simple example to download an image using requests:
Downloading large files with streaming
www.cronistalascolonias.com.ar_content
In the above code, all content of the image will be read into memory at once. If the image is large, it may consume too much memory.
Alternatively, we can set parameter to to stream request. In this case, only the response header is downloaded. We can retrieve the image in a whole using 1 or chunk by chunk by using method:
www.cronistalascolonias.com.ar
When is , we can also use to stream the download. is a file-like object. With the help of , we can save the image like this:
You may want to avoid this for large files! ︎
Authorjdhao
LastMod
LicenseCC BY-NC-ND
0 thoughts to “Urllib to download a file”