Downloading files
We can make use of the requests
Python module to download files. The requests
module is a simple and easy-to-use HTTP library in Python that has various applications. Also, it helps establish the seamless interaction with the web services.
Getting ready
First of all, you have to install the requests
library. This can be done using pip
by typing the following command:
pip install requests
How to do it...
Let's try downloading a simple image file with the requests
module. Open Python 2:
- As usual, import the
requests
library first:
>>> import requests
- Create an HTTP response object by passing a URL to the
get
method:
>>> response = requests.get("https://rejahrehim.com/images/me/rejah.png")
- Now send the HTTP request to the server and save it to a file:
>>> with open("me.png",'wb') as file:... file.write(response.content)
If it's a large file, the response.
content
will be a large string and won't be able to save all the data in a single string. Here...