To download a file from a URL using Python, use the requests.get() method. For example, let’s download Instagram’s icon:
import requests
URL = "https://instagram.com/favicon.ico"
response = requests.get(URL)
open("instagram.ico", "wb").write(response.content)
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.
- Write the file 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:
pip install requests
Then, you can use it to download the icon behind the URL:
# 1. Import the requests library
import requests
URL = "https://instagram.com/favicon.ico"
# 2. download the data behind the URL
response = requests.get(URL)
# 3. Open the response into a new file called instagram.ico
open("instagram.ico", "wb").write(response.content)
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:
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:
pip install wget
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:
import wget URL = "https://instagram.com/favicon.ico" response = wget.download(URL, "instagram.ico")
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:
pip install urllib
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:
from urllib import request
URL = "https://instagram.com/favicon.ico"
response = request.urlretrieve("https://instagram.com/favicon.ico", "instagram.ico")
As a result of running the code, you can see an Instagram icon appear in the folder of your program.
Thanks for reading. Happy coding!