How to Download Files from Urls With Python
Last Updated :
28 Apr, 2025
Here, we have a task to download files from URLs with Python. In this article, we will see how to download files from URLs using some generally used methods in Python.
Download Files from URLs with Python
Below are the methods to Download files from URLs with Python:
Download Files From URLs Using 'requests' Module
In this example, below Python code utilizes the `requests` library to download a PDF file from a specified URL (`'https://media.geeksforgeeks.org/wp-content/uploads/20240226121023/GFG.pdf'`). It checks the HTTP response status code, and if it's 200 (OK), the content is saved to a local file named 'research_Paper_1.pdf'.
Python3
import requests
url = 'https://media.geeksforgeeks.org/wp-content/uploads/20240226121023/GFG.pdf'
response = requests.get(url)
file_Path = 'research_Paper_1.pdf'
if response.status_code == 200:
with open(file_Path, 'wb') as file:
file.write(response.content)
print('File downloaded successfully')
else:
print('Failed to download file')
Output:
File downloaded successfully
Download Files From Urls Using 'urllib3' module:
In this example, below Python code uses the `urllib.request` module to download a PDF file from the specified URL (`'https://media.geeksforgeeks.org/wp-content/uploads/20240226121023/GFG.pdf'`). The `urlretrieve` function saves the content to a local file named 'research_Paper_2.pdf'.
Python3
import urllib.request
url = 'https://media.geeksforgeeks.org/wp-content/uploads/20240226121023/GFG.pdf'
file_Path = 'research_Paper_2.pdf'
urllib.request.urlretrieve(url, file_Path)
Output:
('research_Paper_2.pdf', <http.client.HTTPMessage at 0x7e5a2d16dcc0>)
Download Files From Urls Using 'wget' Module
In this example, below Python code utilizes the `wget` module to download a PDF file from the specified URL (`'https://media.geeksforgeeks.org/wp-content/uploads/20240226121023/GFG.pdf'`). The `wget.download` function saves the content to a local file named 'research_Paper_3.pdf'.
Python3
import wget
url = 'https://media.geeksforgeeks.org/wp-content/uploads/20240226121023/GFG.pdf'
file_Path = 'research_Paper_3.pdf'
wget.download(url, file_Path)
print('downloaded')
Output:
downloaded
Conclusion
In conclusion, Python provides multiple convenient methods for downloading files from URLs, catering to a range of preferences and project requirements. Whether using the straightforward urllib.request
, the versatile requests
library, or specialized modules like wget
and urllib3
, developers have the flexibility to choose an approach that best suits their needs. Armed with these techniques, efficiently incorporating file downloads into Python applications becomes a seamless and accessible task.
Similar Reads
Downloading files from web using Python Requests is a versatile HTTP library in python with various applications. One of its applications is to download a file from web using the file URL. Installation: First of all, you would need to download the requests library. You can directly install it using pip by typing following command: pip ins
4 min read
How to download Files with Scrapy ? Scrapy is a fast high-level web crawling and web scraping framework used to crawl websites and extract structured data from their pages. It can be used for a wide range of purposes, from data mining to monitoring and automated testing. In this tutorial, we will be exploring how to download files usi
8 min read
How To Upload And Download Files From AWS S3 Using Python? Pre-requisite: AWS and S3Amazon Web Services (AWS) offers on-demand cloud services which means it only charges on the services we use (pay-as-you-go pricing). AWS S3 is a cloud storage service from AWS. S3 stands for 'Simple Storage Service. It is scalable, cost-effective, simple, and secure. We gen
3 min read
How to use R to download file from internet ? In this article, we will be looking at the approach to download any type of file from the internet using R Programming Language. To download any type of file from the Internet download.file() function is used. This function can be used to download a file from the Internet. Syntax: download.file(url,
2 min read
How to download an image from a URL in Python Downloading content from its URL is a common task that Web Scrapers or online trackers perform. These URLs or Uniform Resource Locators can contain the web address (or local address) of a webpage, website, image, text document, container files, and many other online resources. It is quite easy to do
3 min read