To download a file from a URL in Python, use the requests.get() method. The requests.get() is a library method that sends a GET request to the specified URL. The requests package is a third-party, simple, and elegant HTTP library in Python that provides support to send GET or POST requests to external resources.
Downloading multiple files from the Internet manually as part of your everyday practice can truly be a nightmare. And if you’re looking for a way to automate your file downloads, then Python’s Wget is the right tool for you.
To work with the requests package, you need to install it in your local system. To install requests in Python, type the following command.
python3 -m pip install requests
It will install the requests package in your system.
Program to download a file from URL in Python
import requests URL = "https://www.facebook.com/favicon.ico" response = requests.get(URL) open("facebook.ico", "wb").write(response.content)
It will download the facebook’s favicon.ico file in your current working directory.
In this program, we imported a requests package that handles the GET or POST requests. In the next line, we specified the link for the image file. We provided the link for the Facebook icon image.
We use requests.get() function to send a get request to the URL link. We get the content from the URL. We open a file in write binary mode and write the contents from the URL to the file.
We created a file named facebook.ico and have written the data into the file. Hence you can see how to use the response package to download the file.
Download a file from URL using wget in Python
The Wget is a non-interactive tool that can be used to download remote files from the internet. It is built-in with Unix-based OS and now it has a version built for Windows OS too.
To install wget in Python:
python3 -m pip install wget
After installing, now we can use the wget.download() function to download a file from a server.
import wget URL = "https://www.facebook.com/favicon.ico" response = wget.download(URL, "facebook.ico")
It will download the facebook’s facebook.ico file in your current working directory.
In this program, we imported a package called the wget using the import statement. To import a package in Python, use the import statement.
In the next line, we use the wget.download() function and pass the URL of the file to the function. Then, we downloaded the file using a function called wget.download().
The wget.download() function accepts the first parameter as the URL link and the next one is the file name you want to store it as. We then store the content in a variable called the response.
Download a file from a URL using the urllib package
The urllib.request module defines functions and classes which help in opening URLs. The urllib.request.urlretrieve() function accepts a URL as an argument and the second argument is the file name you want to store it as.
from urllib import request URL = "https://www.facebook.com/favicon.ico" response = request.urlretrieve("https://www.facebook.com/favicon.ico", "facebook.ico")
If you run the above file, It will download the facebook’s favicon.ico file in your current working directory as the name of the “facebook.ico” file.
In this program, we imported a package known as the urllib. It is a built-in Python package. This package is used for working with urls.
From urllib, we have imported a submodule called the request. Then, we provided the url for Facebook’s favorite icon image. We can send a GET request and get the data using the function request.urlretireve(). The request.urlretrieve() function is used to retrieve the data from the url.
The first argument is the url and the second is the file name you want to save. We store the content in a variable called the response.
In this tutorial, we saw three ways you can download a file from a URL in Python:
- Using requests.get() function.
- Using wget.download() function.
- Use urllib.request.urlretrieve() method.
That’s it for this tutorial.
Krunal Lathiya is an Information Technology Engineer. By profession, he is a web developer with knowledge of multiple back-end platforms including Python. Krunal has written many programming blogs which showcases his vast knowledge in this field.