Python HTTP File Download: Using the Requests Library

Download File Over HTTP

Python is not only about carrying out complex computations, it also has a bag full of tricks when it comes to getting things done through the web. With Python, you can easily extract specific information from any publicly accessible domain. In this article, we shall set out to explore one such feature of Python that can be used to download files over HTTP using an exclusive library – the Requests!

  • Why use Requests library?
  • Installing Requests in Python
  • Downloading a file using Requests

Advantages of the Requests Library

A ‘request’ is when you ask for some data from a server, and a ‘response’ is what the server gives back to you.

In short, the requests library aids you in sending HTTP requests through Python programming. Requests do not emphasize the need to add any query strings to the desired URL, thereby making it one of the easiest techniques to download files from the web. When a request is made using any particular URL, what we get in return is a response.

To put a cheery on the top, this library has a rich palette of built-in features that assists in the management of not only the requests thrown but also the responses received. Requests is all flexible when it comes to handling APIs or downloading webpages as a whole which makes it a go-to choice for web scraping.


How to Install the Requests Library in Python

Now that we know how critical the deployment of the requests library is, let us get started with the steps to install the same. Hit that windows key on your keyboard and type cmd to open the command prompt. Once done, change the directory if necessary or get started with the installation of the requests module by running the following code.

pip install requests

It is only a matter of seconds before all the necessary packages are downloaded and the requests library becomes palpable at your disposal.


How to Download a File Using the Requests Library

With the requests module being successfully installed, let us now start to use it by importing in Python through the following code.

import requests

Now let us try to import an article from our very own AskPython which details the bit manipulation techniques. For this, one needs to get the URL of the webpage which contains the respective article. Given below is the URL of the article.

https://www.askpython.com/python/examples/python-bit-manipulation-masking-techniques

It is about time to map a variable for downloading the contents of this webpage as a file through the requests.get( ) function. To understand it better, let us have a look at its syntax.

requests.get(URL, params, args)

where,

  • URL – used to specify the web address of the desired page
  • params – used to specify the key:value pairs
  • args – used to specify the arguments to be passed

Let us now feed in the URL to the above function through the below-mentioned code.

ip = requests.get('https://www.askpython.com/python/examples/python-bit-manipulation-masking-techniques')

Run the code and to confirm whether the execution was successful, one can either run or print the variable ‘ip’ to see if it returns a [200] response. On the other hand, one can also run or print ‘ip.content’ to see if it shows a squeezed text button as shown below.

Successful Execution Of Requests Get Function
Successful Execution of requests.get( ) Function

Saving the Downloaded File Locally

Now let us download the file as a ‘.txt’ file using a combination of open and write functions as shown in the code below. It is to be noted that one needs to specify the location in which the file is to be downloaded.

with open(r'C:\Users\test1.txt', 'w') as f:
    f.write(ip.text)

Upon running the above code, one can navigate through the specified path to witness the creation of a text file with the required details from the given URL.

File Successfully Downloaded
File Successfully Downloaded

Conclusion:

Now that we have reached the end of this article, hope it has elaborated on downloading a file over HTTP in Python. Here’s another article that details how to debug the syntax errors from the elif function in Python. There are numerous other enjoyable and equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. What file will you download next with Python?


Reference: