Python How to Download a File from a URL

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)

More Detailed Steps

To download a file from a URL using Python follow these three steps:

  1. Install requests module and import it to your project.
  2. Use requests.get() to download the data behind that URL.
  3. 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:

  1. Import the wget module into your project.
  2. 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:

  1. Import the urllib module into your project.
  2. 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.

Conclusion

Thanks for reading.

Happy coding!

Further Reading

How to Write to a File in Python

How to Read a File in Python

Share on facebook
Facebook
Share on google
Google+
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on pinterest
Pinterest

Leave a Comment

Your email address will not be published.