How to Download a File From URL in Python: 3 Easy Methods

Want to know how can you download a file from a URL in Python? Let me tell you!

How to Download a File from URL in Python?

In Python, there are various methods to download a file from a URL in case you want to retrieve an image, a document, or any other type of file.

Below are some of the simplest methods to accomplish this task:

Let’s discuss them individually.

Method 1: Using urllib.request

In Python, urllib.request enables programmers to open and read URLs. It is part of the urllib package, which handles various URL-related tasks like fetching data across the web, parsing URLs, etc.

Here is how you can utilize this module to download a file from the URL:

  • First of all, import the “urllib.request” into the code.
  • Then, specify the file’s URL and store it in a variable.
  • Create another variable holding the file’s local name.
  • Lastly, call the “urlretrieve()” method with the file’s URL and name.
import urllib.request
url = 'https://filesampleshub.com/download/document/pdf/sample1.pdf'
file_name = 'new_file.pdf'
urllib.request.urlretrieve(url, file_name)
print("File Downloaded")
downloading file from url using urllib.request in python

Result:

pdf file downloaded from url in python

Method 2: Using requests

The requests Python library is utilized for making HTTP requests. It simplifies the process of sending HTTP requests, handling responses, and working with web services.

This library can also be used to download a file from a URL in Python. Here’s how:

To download this library, open the command prompt or Python IDEs terminal and run this command:

pip install requests
installing requests library using pip

Now:

  • Import the library first.
  • After that, store the file URL and local name into separate variables.
  • Send a GET request to the URL to fetch the image and save the response from the server in a variable.
  • Then, open the file with its local name, in “wb” (write-binary) mode, and write the image data (binary format) into it.
import requests
url = 'https://filesampleshub.com/download/image/png/sample1.png'
file_name = 'sample_img.png'
response = requests.get(url)
with open(file_name, 'wb') as file:
   file.write(response.content)
print("File Downloaded")

downloading file from url using requests in python

Result:

image downloaded from url in python

Method 3: Using wget

In Python, you can use the wget library to download files from the web easily. It operates similarly to the open-source wget tool.

Firstly, get it on your system via the mentioned command:

pip install wget
installing wget using pip
  • Import it into code and define a variable holding the file’s URL you want to download.
  • Finally, download the file by passing its URL. 
import wget
url = 'https://filesampleshub.com/download/code/html/sample1.html'
file_name = wget.download(url)
print("File Downloaded")
downloading file from url using wget in python

Result:

html file downloaded from url in python

Conclusion

To download a file from a URL in Python, you can either use the “urllib.request” module, “requests” library, or “wget” library. The first one is a part of Python’s standard library and you can use it straight away.

If you want to utilize the rest of the two approaches then you have to install the respective libraries using pip.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Scroll to Top