Yuna Luzi
Yuna Luzi

Reputation: 338

Download Zip Folder with Python 3.5 using Urllib

I am trying to download a list of zip folders from a website using urllib and python 3.5. Urllib.request.urlretrieve documentation says you can retrieve files but not zip folder. Here is an example of the url: https://www.inegi.org.mx/contenidos/programas/enoe/15ymas/microdatos/2005trim1_dta.zip. Most examples show read/write to new files which does not work for the url above because the folder has five files.

Any help would be much appreciated!

Upvotes: 0

Views: 231

Answers (1)

rmonvfer
rmonvfer

Reputation: 78

If there is not any particular reason for you to use urrlib, you should definitively check requests, which is a library specifically built to solve issues like the one you have.

Here is a simple function you can use to download almost any file using the requests library

import requests

def downloadFile(url):
    local_filename = url.split("/")[len(url.split("/")) -1]
    req = requests.get(url, stream=True)
    with open(local_filename, 'wb') as fl:
        for chunk in req.iter_content(chunk_size=1024): 
            if chunk: 
                fl.write(chunk)

# The file will be saved as "2005trim1_dta.zip"
downloadFile("https://www.inegi.org.mx/contenidos/programas/enoe/15ymas/microdatos/2005trim1_dta.zip")

Note how I used requests.get() to download the file.

If you are interested in requests, I recommend you to check its documentation at http://docs.python-requests.org/en/master/ as it is very clear and concise

Upvotes: 1

Related Questions