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

enter image description here

But when I open link via the original website i.e 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)

Upvotes: 0

Views: 327

Answers (1)

Sergey Karbivnichiy
Sergey Karbivnichiy

Reputation: 116

You need to send referer headers:

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

...

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

Upvotes: 3

Related Questions