Download Files Using Python Requests Library

If you’re looking to download a file using Python, the requests library is a great option to consider. This library allows you to easily make HTTP requests and handle responses in a Pythonic way.

First, you’ll need to install the requests library if you haven’t already. You can do this using pip , the package installer for Python.

pip install requests

Once you have requests installed, you can use it to make a GET request to the URL of the file you want to download. This will return a Response object that contains the content of the response, including the file you want to download.

Next, you’ll need to write the content of the response to a file on your local machine. You can do this by opening a file in write binary mode and writing the content of the response to it. Once the file has been written, you can close the file and you should have successfully downloaded the file using Python and requests.

Prerequisites

In order to download a file with Python requests, you will need to have the following:

  • A basic understanding of Python programming language
  • Python installed on your computer
  • The requests library installed in Python

If you are not familiar with Python, it is recommended that you first learn the basics of the language before proceeding with this tutorial. Python is a popular programming language that is easy to learn and has a wide range of applications.

Python can be downloaded from the official website, and installation instructions can be found there as well. The requests library can be installed using pip , which is a package installer for Python. Simply type pip install requests in your command prompt or terminal to install it.

Installing Requests

Installing Requests is a straightforward process that can be accomplished with pip, the Python package installer.

You can install requests by running the following command in your terminal:

pip install requests

Once you’ve installed requests , you can verify that it’s working by opening a Python REPL and typing the following:

>>> import requests
>>> res = requests.get("https://www.google.com")
>>> print(res.status_code)
200

If everything is working correctly, you should see a 200 status code as shown in the above code printed to your console.

If you see a different status code, it means that there’s an issue with your installation of requests .

Download File Using requests

Python’s requests library is a powerful tool for making HTTP requests. It also makes it easy to download files from the web. Here’s how:

First, import the requests library:

import requests

Next, use the get method to make a request for the file:

response = requests.get(url)

Replace url with the URL of the file you want to download.

You can further check the response by determining if everything is ok , by writing:

import requests
url = "URL download file url"

response = requests.get(url)
if response.ok:
    # everything seems ok, let's save the file

Saving Downloaded File

After successfully downloading the file using Python requests, the next step is to save it to your local machine. This can be done using the `write` method of the file object in Python.

First, you need to open a new file in write-binary mode using the open function. Then, you can write the contents of the downloaded file to the new file using the write method. Finally, you need to close the file which will happen automatically if everything is wrapped in a with statement as follows:

with open(filename, 'wb') as f:
    f.write(response.content)

Replace filename with the name you want to give the file. The 'wb' argument tells Python to open the file in binary mode so that it can write the file’s contents.

That’s it! You’ve successfully downloaded a file using Python’s requests library.

Here is an example code snippet that shows how to save a downloaded file:

import requests

url = 'https://example.com/image.jpg'
response = requests.get(url)

if response.ok:
    with open('image.jpg', 'wb') as f:
        f.write(response.content)
else:
    print(f"{response.status_code} fetching {url}")

In this code above, I first send a GET request to the URL of the file I want to download. I check to make sure everything is ok and if so proceed to saving the contents of the file downloaded.

To save the file, I open a new file named image.jpg in write-binary ( 'wb' ) mode using the open function. I wrote the contents of the downloaded file to this new file using the write method.

If there were problems, I printed the result to the console to see the status_code (i.e. if it returns a 404) and the URL.

Error Handling

When downloading files with Python requests, it’s important to handle errors that may occur during the process.

It can be easy to mistype the URL or for the URL to be a redirect.

Therefore, you will want to expose the status_code to see if you’re getting 404 or 301 type responses.

If you are getting 301 type redirects then you might want to amend your .get() request to allow for redirects, like so:

requests.get("http://example.com/image.jpg", allow_redirects=True)

Conclusion

Using Python’s requests library, downloading files from the internet has never been easier. By using the .get() method and specifying the URL, you can retrieve the content of the file and save it locally using the file.write method.

In conclusion, Python’s requests library is a powerful tool for downloading files from the internet. It provides a simple and intuitive interface, making it easy to retrieve and save files with just a few lines of code.

Photo of author
Ryan Sheehy
Ryan has been dabbling in code since the late '90s when he cut his teeth exploring VBA in Excel. Having his eyes opened with the potential of automating repetitive tasks, he expanded to Python and then moved over to scripting languages such as HTML, CSS, Javascript and PHP.