简体   繁体   中英

Downloading zip files with python

Using a simple code to download zip files

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)

url = 'https://www1.nseindia.com/content/historical/EQUITIES/1994/NOV/cm03NOV1994bhav.csv.zip'
save_path = 'D:/folder/Programming/Python/trading/Bhavcopy/bhavcopy.csv.zip'

download_url(url,save_path)

The end result is the creation of an invalid zip file. I tried to open the website by manually pasting the url on browser and got this

在此处输入图片说明

But when I open link via the original website ie going the nse website and clicking button to download, the link works.

Additional data

Here is the link from where you try downloading the file for yourself. https://www1.nseindia.com/products/content/equities/equities/archieve_eq.htm

I'm donwloading the files from first option(Bhavcopy) for the first date for which it is available (3rd Nov 1994)

You need to send referer headers:

headers = {'Referer':'https://www1.nseindia.com'}

...

r = requests.get(url, stream=True,headers=headers)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM