Python download zip file from url

Most people recommend using requests if it is available, and the requests documentation recommends this for downloading and saving raw data from a url:

import requests 

def download_url[url, save_path, chunk_size=128]:
    r = requests.get[url, stream=True]
    with open[save_path, 'wb'] as fd:
        for chunk in r.iter_content[chunk_size=chunk_size]:
            fd.write[chunk]

Since the answer asks about downloading and saving the zip file, I haven't gone into details regarding reading the zip file. See one of the many answers below for possibilities.

If for some reason you don't have access to requests, you can use urllib.request instead. It may not be quite as robust as the above.

import urllib.request

def download_url[url, save_path]:
    with urllib.request.urlopen[url] as dl_file:
        with open[save_path, 'wb'] as out_file:
            out_file.write[dl_file.read[]]

Finally, if you are using Python 2 still, you can use urllib2.urlopen.

from contextlib import closing

def download_url[url, save_path]:
    with closing[urllib2.urlopen[url]] as dl_file:
        with open[save_path, 'wb'] as out_file:
            out_file.write[dl_file.read[]]

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

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:

Installing the wget library

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 “//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='//golang.org/dl/go1.17.3.windows-amd64.zip'
wget.download[url]

You will see the progress in the output console.

Downloading a zip file using wget

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 '//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

Installing the requests module

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 ‘//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 = '//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.

Downloading a zip file using the requests module

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 = '//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']

Downloading and extracting a zip file using python

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['//sample-videos.com/download-sample-zip.php']
baseurl= '//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']

Multiple Zip Files Download

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['//sample-videos.com/download-sample-zip.php']
baseurl= '//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:

Download all the zip files from a URL

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:

Download zip file link in Github

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='//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

Downloading the Azure storage library

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.

Azure storage access keys

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="

Chủ Đề