Using Python, you can program a lot of tasks due to its rich library. One of the tasks that you can do using a Python program is to download a zip file from a URL.
In this Python article, you will see multiple examples of How to download zip file from URL using python.
- Python wget download zip file
- Python requests module to download zip file
- Python download zip file from URL and extract
- Python download multiple zip files from URL
- Python download zip file from Github
- Download zip file from Azure blob storage python
Python wget download zip file
One way to download a zip file from a URL in Python is to use the wget() function. But you need to install the wget library first using the pip command-line utility.
In your command prompt, execute the below code to install the wget library:
pip install wget
Once installed, you will see an output like the below screen:
Now you can use the wget library to download a zip file. The below is the syntax to use the wget function.
wget('URL of the zip file')
For example, the link to download the zip source file for golang is “https://golang.org/dl/go1.17.3.windows-amd64.zip”. I can execute the below Python code snippet to download this zip file:
import wget
url='https://golang.org/dl/go1.17.3.windows-amd64.zip'
wget.download(url)
You will see the progress in the output console.
You can see your zip file in the same folder in which you have saved the Python source code file.
You can also use the wget option in an alternative way. The same zip file can be downloaded by executing the below code in the command line:
python -m wget 'https://golang.org/dl/go1.17.3.windows-amd64.zip' -o 'C:\Users\Blades\Downloads'
Using the -o flag, you can specify the download path for the zip file.
Hence, in this way you can use the wget library in Python to download a zip file from a URL.
Read: Python Return Function
Python requests module to download zip file
You can also download a zip file from a URL using the requests module. We have to send a request to the zip file URL and store the results in a variable. Then we can write this zip content into the local file system.
But. firstly, you have to install this module into your Python compiler using the pip command:
pip install requests
Now you can just import the module in your code and start using it.
For example, if I have to download a zip file from the URL ‘https://golang.org/dl/go1.17.3.windows-amd64.zip’, I can write the below Python code snippet:
# importing the requests module
import requests
print('Downloading started')
url = 'https://golang.org/dl/go1.17.3.windows-amd64.zip'
# Downloading the file by sending the request to the URL
req = requests.get(url)
# Split URL to get the file name
filename = url.split('/')[-1]
# Writing the file to the local file system
with open(filename,'wb') as output_file:
output_file.write(req.content)
print('Downloading Completed')
In the above code, we are sending a GET request to the specified URL and returning the zip file content in response. After that, we have written the zip file content into a file in our local file system.
You can verify the download in the location of your Python source code file. Thus, you might have learned how you can download a zip file from a URL in Python using the requests module.
Read: Python find index of element in list
Python download zip file from URL and extract
In this section, you will learn how you can download and extract a zip file into your local file system.
We will need three modules:
- requests: To download the zip file
- BytesIO: To read the file from the buffer
- zipfile: To read and extract the zip file
For example, the below python code snippet will download a zip file from the specified URL and extract the zip file into the local file system.
# importing necessary modules
import requests, zipfile
from io import BytesIO
print('Downloading started')
#Defining the zip file URL
url = 'https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-zip-file.zip'
# Split URL to get the file name
filename = url.split('/')[-1]
# Downloading the file by sending the request to the URL
req = requests.get(url)
print('Downloading Completed')
# extracting the zip file contents
zipfile= zipfile.ZipFile(BytesIO(req.content))
zipfile.extractall('C:/Users/Blades/Downloads/NewFolder')
In the above code, firstly we are downloading the zip file and storing its contents into a variable.
Then we are using the BytesIO function to read the zip file contents and store them in a variable in the zip format.
Finally, we are using the extractall() function to extract the zip file data into the local file system.
Note: While defining the file path in the extractall() function, use forward slashes(/) instead of backward slashes(\). Otherwise, you will face an error.
Alternatively, you can also append r before the path to make it a raw string. For example:
zipfile.extractall(r'C:\Users\Blades\Downloads\NewFolder')
Thus, you might have learned how you can download and extract a zip file from a URL in python.
Read: Python find number in String
Python download multiple zip files from URL
In this section, I will explain how you can download multiple zip files from a URL.
You may face a scenario when you want to download all the zip files from a single web page with the help of Python. In that case, you can use the following technique to download all the zip files:
- We have used the requests module to perform this task. You can install this module with the pip command in python.
- Secondly, we have used the BeautifulSoup library to clean the response content of a webpage.
- The first step is to fetch the webpage in where all the links to the zip file appears.
- Then I have cleaned the response of the request using BeautifulSoup library.
- After that, I have fetched all the URLs of the zip files and stored them in a text file.
- Once all the links are stored in the text file, I am downloading every zip file by reading the URL from the text file and sending a GET request to the URL.
- As a result, a file is downloaded in the response.
- I have created a new zip file in the local file system and writing the zip file contents downloaded in the previous step.
# importing the necessary modules
import requests
from bs4 import BeautifulSoup
# Creating a new file to store the zip file links
newfile = open('zipfiles.txt','w')
#Set variable for page to be opened and url to be concatenated
page =requests.get('https://sample-videos.com/download-sample-zip.php')
baseurl= 'https://sample-videos.com/'
#Use BeautifulSoup to clean up the page
soup = BeautifulSoup(page.content)
soup.prettify()
#Find all the links on the page that end in .zip and write them into the text file
for anchor in soup.findAll('a', href=True):
links = anchor['href']
if links.endswith('.zip'):
newfile.write(links + '\n')
newfile.close()
#Fetching the links for the zip file and downloading the files
with open('zipfiles.txt', 'r') as links:
for link in links:
if link:
filename1= link.split('/')[-1]
filename= filename1[:-1]
link = baseurl + link
print(filename + ' file started to download')
response = requests.get(link[:-1])
# Writing the zip file into local file system
with open(filename,'wb') as output_file:
output_file.write(response.content)
print(filename + 'file is downloaded')
Once the program is executed successfully, you will see that all the zip files are downloaded in your Python source code location.
Alternative Method:
There is also another method to do this i.e using the wget() function.
You have to install the wget library using the pip command.
After that you can execute the below code to download all the zip files from a URL:
# importing the necessary modules
import requests
from bs4 import BeautifulSoup
import wget
# Creating a new file to store the zip file links
newfile = open('zipfiles.txt','w')
#Set variable for page to be opened and url to be concatenated
page =requests.get('https://sample-videos.com/download-sample-zip.php')
baseurl= 'https://sample-videos.com/'
#Use BeautifulSoup to clean up the page
soup = BeautifulSoup(page.content)
soup.prettify()
#Find all the links on the page that end in .zip and write them into the text file
for anchor in soup.findAll('a', href=True):
links = anchor['href']
if links.endswith('.zip'):
newfile.write(links + '\n')
newfile.close()
#Fetching the links for the zip file and downloading the files
with open('zipfiles.txt', 'r') as links:
for link in links:
if link:
link = baseurl + link
wget.download(link[:-1])
The approach is almost the same as the above approach. The only difference is that we are using the wget library to download the file instead of the requests library.
The benefit of using the wget library is that you can also see the progress bar of your download as shown in the below image:
Thus, you might have learned how you can download all the zip files from a single web page using python.
Read: Remove non-ASCII characters Python
Python download zip file from Github
In this section, I will explain how you can download a zip file from Github using python.
In any Github repository, you have the option to download the source code as a zip file. Look at the below image for reference:
You can use this link in your Python program to download this zip file. You can use any of the methods explained in the above sections. For example, you can use wget() to download the zip file using the below Python code:
import wget
url='https://github.com/Blades007/android_weather/archive/refs/heads/main.zip'
wget.download(url)
Once the code is executed successfully, you can see the file is created in the folder where you have stored the python source code file.
In this way, you can download a zip file from Github using Python.
Read: Python convert binary to decimal
Download zip file from Azure blob storage python
To work with the Azure blob storage in Python, you need to install a library named azure-storage-blob. To install this library in your Python compiler, execute the below command in your command prompt:
pip install azure-storage-blob
Once you have installed this library you can write a code to download a zip file from the Azure blob container. The process is explained below:
- Firstly, we will make a connection with the file stored in the Azure storage container using a connection string.
- Then, we will download the file from the storage.
- Finally, we will save the file in our local file system.
To get the connection string for your Azure storage container:
- Navigate to your Azure storage container and click on Access Keys in the Security + Networking tab.
- You will see a list of keys. But they all will be hidden.
- Click on Show Keys to view the keys.
- The connection strings of the keys are listed with them. You can use any of the connection strings.
Now you can execute the below Python code to download a zip file stored in the Azure container.
# importing the necessary module
from azure.storage.blob import BlobClient
# making a connection with the file in Azure blob storage container
blob = BlobClient.from_connection_string(conn_str="<your connection string", container_name="<your azure storage container name>", blob_name="your zip file name")
# writing the zip file to the local file system
print("Download Started")
with open("./sample-zip-file.zip", "wb") as my_blob:
blob_data = blob.download_blob()
my_blob.write(blob_data.content_as_bytes())
print("Download is complete")
Substitute your credentials in the above code.
Note: The data is being downloaded in the form of bytes and written in a new file. You can give any name to your new zip file.
You can see that the zip file is downloaded in the specified path.
In this way, you can download a zip file from your Azure storage container to your local file system.
Also, take a look at some more tutorials on Python.
- Python Count Words in File
- Case statement in Python
- Python Screen Capture
- Python Pretty Print JSON
- Python dictionary increment value
- How to Reverse a List in Python
Thus, you might have learned the various ways to download zip file from URL using Python.
- Python wget download zip file
- Python requests module to download zip file
- Python download zip file from URL and extract
- Python download multiple zip files from URL
- Python download zip file from Github
- Download zip file from Azure blob storage python
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.