Hello guys, in this tutorial i am going to show you how you can download file from internet. You will also learn to download different types of files such as PDF, HTML, ZIP etc. You will also learn to create a file downloader with a nice progress bar for your terminal using python. So let’s start Python Download File Tutorial.
One of the most important and common programming tasks to perform on the web is downloading files from different online resources. A huge number of successful applications allow users to download files. So these are just a few web application functions that require downloading files:
- File sharing
- Retrieving website code (CSS, JS, etc)
- Data mining
- Social media
Contents
Python Download File – Most Popular Ways To Download Files Using Python
So guys there are many ways to download files using python. Let’s see them one by one.
requests Module
Using requests module is one of the most popular way to download file. So first of all you need to install requests module, so run the following command on your terminal.
1 2 3 |
pip install requests |
So now write the following code for downloading files using requests module.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import requests print('Download Starting...') url = 'http://www.tutorialspoint.com/python3/python_tutorial.pdf' r = requests.get(url) filename = url.split('/')[-1] # this will take only -1 splitted part of the url with open(filename,'wb') as output_file: output_file.write(r.content) print('Download Completed!!!') |
- Firstly import the requests module for performing HTTP request over the internet.
- Then declare a url from where you want to download your file.
- get( ) method of the requests module is used to download the file contents in binary format.
- Now you have to open that filename in write binary(wb) mode.
- output_file.write(r.content) means whatever the url contains, it will get all those contents and it will write all those to output_file.
- Finally print Download Completed.
Now run this program and see what happens. Your file is successfully downloaded, now check this file in your ‘Download’ folder.
urllib.request Module
You can also use urllib.request module to download file over HTTP.
- urlretrieve method of this module is used to download the file from internet.
Now write the following code.
1 2 3 4 5 6 7 |
import urllib.request url = 'http://www.tutorialspoint.com/python3/python_tutorial.pdf' urllib.request.urlretrieve(url, r'C:\Users\saba\Desktop\python1.pdf') |
Explanation
- First of all, you have to install urllib.request module.
- Then you have to create a variable that will contain the url of the file which you want to download.
- Then call the urlretrieve( ) method. You have to pass two arguments to this method, the first one is url and the second one is the file path where you want to locate your file.
- One thing should be keep in mind that you can pass any filename as the second parameter.
Now run the above code and check your download folder, you will see the file has been downloaded.
And now its time to move another section of this tutorial that is how to download different types of files such as text, html, pdf, image files etc using python.
Python Download File Tutorial – Downloading PDF, HTML, Image And Text files
In this section, you will see how to download different types of file.
Downloading PDF File
Write the following code to download PDF file.
1 2 3 4 5 6 7 8 9 10 |
# Import urllib.request module import urllib.request # Create a variable and pass the url of file to be downloaded url = 'http://www.tutorialspoint.com/python3/python_tutorial.pdf' # Copy a network object to a local file urllib.request.urlretrieve(url, r'C:\Users\saba\Desktop\python1.pdf') |
Now check your download location, you will see your file has been successfully downloaded.
Downloading HTML File
Write the following code for downloading an HTML file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# import requests library import requests print('Download Starting...') # Create a variable and pass the url of file to be downloaded url = 'https://pypi.org/project/wget/' # download the file contents in binary format req = requests.get(url) #open method will open a file on your system and write the contents with open('/Users/saba/Desktop/wget.html', 'wb') as f: f.write(req.content) print("Download Completed!!!") |
Let’s check your download location.
Downloading image File
Write the following code to download an image file.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import requests print('Download Starting...') url = 'https://www.python.org/static/img/python-logo@2x.png' req = requests.get(url) with open('/Users/saba/Desktop/python.png', 'wb') as f: f.write(req.content) print("Download Completed!!!") |
Let’s Check A-Star Algorithm Python Tutorial – An Introduction To A* Algorithm In Python
Downloading Youtube Video File
- For downloading youtube video, you have to install pytube module.
- Run the following command on your terminal.
1 2 3 |
pip install pytube |
And now write the following code.
1 2 3 4 5 |
from pytube import YouTube YouTube('https://www.youtube.com/watch?v=Y8Tko2YC5hA').streams.first().download('/Users/saba/Desktop') |
- Now at first pass the url.
- streams.first( ) method grabs the first format of the video.
- download() method downloads the video.
Now run your code and you will see the video has been downloaded to your passed location.
Downloading Text File
Write the following code for download a text file.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import requests print('Download Starting...') url = 'https://pypi.org/project/wget/' r = requests.get(url) with open('/Users/saba/Desktop/mytext.txt', 'wb') as f: f.write(r.content) print("Download Completed!!!") |
Now check the download location, you will see text file has been downloaded.
Download Zip File
Write the following code to download zip file.
1 2 3 4 5 6 7 8 9 10 11 |
# import the requests library import requests url = 'https://publib.boulder.ibm.com/bpcsamp/v6r1/monitoring/clipsAndTacks/download/ClipsAndTacksF1.zip' # download the file contents in binary format r = requests.get(url) with open("/Users/saba/Desktop/myzip.zip", "wb") as zip: zip.write(r.content) |
Now check the download location, you will see a zip file has been downloaded.
Also Read – Sets In Python Tutorial For Beginners
Python Download File – Downloading Large Files In Chunks, And With A Progress Bar
In this section, we will see how to download large files in chunks, download multiple files and download files with a progress bar. So let’s start.
Downloading Large Files In Chunks
You can also download large files in chunks. Its very easy, so let’s check it. Write the following program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import requests print('Download Starting...') url = 'http://www.tutorialspoint.com/python3/python_tutorial.pdf' req = requests.get(url, stream=True) with open("pytutorial.pdf",'wb') as output_file: for chunk in req.iter_content(chunk_size=1025): if chunk: output_file.write(chunk) print('Download Completed!!!') |
- First of all import requests library.
- Then specify url from where you want to download the file.
- Now call the get( ) method and pass stream = True.
- Now create a file whatever name you want to give it and open it in write binary mode.
- Then you have to specify chunk size that you want to download at a time. I have set it 1025 bytes. Now you have to iterate through each chunk and write the chunks in the file until the chunks finished.
- At last print a message Download Completed.
Now run the program, and check your download location, you will found a file has been downloaded.
Downloading File With Progress Bar
Now you will learn how can you download file with a progress bar. First of all you have to install tqdm module. Now run the following command on your terminal.
1 2 3 |
pip install tqdm |
Now write the following code, i will explain it later.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
from tqdm import tqdm import requests chunk_size = 1024 url = "http://www.tutorialspoint.com/python3/python_tutorial.pdf" req = requests.get(url, stream = True) total_size = int(req.headers['content-length']) with open("pythontutorial.pdf", "wb") as file: for data in tqdm(iterable=req.iter_content(chunk_size=chunk_size), total = total_size/chunk_size, unit='KB'): file.write(data) print("Download Completed !!!") |
- First of all import the tqdm and requests module.
- Then specify chunk size that is nothing but small amount of data that you will receive once at a time from the server of that url . Here i am taking chunk size 1024.
- Then specify url from where you want to download your file.
- Now you need to create a response object of request library. So you have to make a HTTP get request. Pass the url and set stream = True to the get( ) method.
- Then define the total size of your file.
- And now you need to create an output file and open it in write binary mode.
- Now start a loop to get content of the response that you have made earlier. Then define tqdm( ) function and inside this define iterable which you are going to use. iterable is nothing but a sequence. And define the chunk size and total size and then unit.
- Then you have to just write data like this file.write(data).
- Finally just print Download Completed message.
Now run the code, you will see progress bar as below on your terminal.
This is very nice. You can see the file size is 2922KB and it only took 49 second to download the file.
Have You Checked – Python Zip File Example – Working With Zip Files In Python
So guys we have successfully completed this Python Download File Tutorial. I hope, you found it helpful if yes then must share with others. And if you have any query regarding this tutorial then feel free to comment. And yes for getting python tutorials stay tuned with Simplified Python. Thanks
What is the “wb” doing?
The wb indicates that the file is opened for writing in binary mode. Only needed in Windows.