How to Download Image from URL using Python
Recently, I want to download some images using Python. This is what I’ve learned after the survey.
Using urllib package
The native and naive way is to use urllib.request module to download an image.
However, the above code may error out with following message:
urllib.error.HTTPError: HTTP Error 403: Forbidden
In this case, we need to add a HTTP header to the request:
Using requests package
A better way is to use requests package. Here is a simple example to download an image using requests:
Downloading large files with streaming
response.iter_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.
Downloading Files over HTTP with Python Requests
Now’s the time to build an ultimate File Downloader that can download large files and has the functionality to resume the broken link… cool ! huh!
So the question is what will be downloading ?? lets, download an image from bing images!!
Now, lets Code !!
first lets import request library and store out image link in img_adrs variable
now, establish a connection with the server hosting the file using re.get() What if the file is of 2GB ? will it work …. NO, because the 2GB response will be stored in RAM which will possibly cause MemoryError
We should chunk up the data by streaming. so it would require to download some Kbs of data only
To store the data of the image(byte sequence) lets create a var data initialized with empty byte string
Now lets loop over the chunks of data using res.iter_content(chunk_size=1024) where each chunk will be of size 1024 bytes and concatenate them in our data var only if the chunk is not empty otherwise it can corrupt our file
Now lets write the byte data into a file :
this will open a file named img.jpg with mode wb that means write in bytes since our data is a byte sequence .
Connection Lost! How to resume the Download ??
firstly don’t panic and follow these steps :
- Check how much data is downloaded
we have downloaded 860 bytes of data … Now the question is how much is left to download ? To find this out lets check the Content-Length parameter of response headers.
this gives us the size of the file in bytes which in our case is around 17kb .So now we know from where to start downloading.
For this task we are going to use <"Range": "bytes=860-">header parameter where we will give the starting point 860 and the end point (which is empty in our case since we want the stream till the end) .
Now after getting the data lets append it into the file.
here we opened the file in ab mode which means append bytes
From these steps U can create your own Universal file Downloader Class from scratch in python .
To get more info about the topic enroll into out Advance Python certification course @ 15% off
Пишем программу для скачивания файлов на Python с использованием requests
Недавно захотел написать программу для изучения библиотеки для Python — requests. Мой выбор пал на написание программы для скачивания файлов.
Поскольку мы будем использовать библиотеку requests, импортируем её (+shutil):
Код «меню» (это заключено в while True для возможности выхода break’ом и работы программы много раз без перезапуска):
В get’е спрашиваем путь к файлу: (причём отдельно директорию и имя файла, зачем это нужно — скажу позже):
А для requests ведь нужен полный путь! Не беда, склеим dirfile и file:
Иногда, чтобы скачать файл, нужно войти. Это нужно реализовать!
Спросим пользователя об этом:
Если нужно входить, тогда напишем запрос с аутентификацией. Спрашиваем логин и пароль:
Python How to Download a File from a URL
To download a file from a URL using Python, use the requests.get() method. For example, let’s download Instagram’s icon:
This is an example for someone who is looking for a quick answer. But if the above code lines don’t work or make sense, please keep on reading.
More Detailed Steps
To download a file from a URL using Python follow these three steps:
- Install requests module and import it to your project.
- Use requests.get() to download the data behind that URL. to a file in your system by calling open().
Here is an example:
Let’s download Instagram’s icon using Python. The icon can be found behind this URL https://instagram.com/favicon.ico.
First, install the requests module by opening up a command line window and running:
Then, you can use it to download the icon behind the URL:
As a result of running this piece of code, you see the Instagram icon appear in the same folder where your program file is.
Other Ways to Download a File in Python
There are other modules that make downloading files possible in Python.
In addition to the requests library, the two commonly used ones are:
- wget
- urllib
How to Download a File Using wget Module
Before you can download files using wget , you need to install the wget module.
Open up a command line window and run:
Then follow these two steps to download a file:
- Import the wget module into your project.
- Use wget.download() to download a file from a specific URL and save it on your machine.
As an example, let’s get the Instagram icon using wget :
As a result of running the code, you can see an Instagram icon appear in the folder of your program.
How to Download a File Using urllib Module in Python
Before you can download files using urllib , you need to install the module. Open up a command line window and run:
Then follow these two steps to download a file:
- Import the urllib module into your project.
- Use urllib‘s request.urlretrieve() method to download a file from a specific URL and save it on your machine.
As an example, let’s get the Instagram icon using urllib :
As a result of running the code, you can see an Instagram icon appear in the folder of your program.