File Downloaded Using Python Requests Module Throwing Invalid File Error

Viewed 149

I have the following code that downloads zip files from a URL. I have used the requests module of Python.

For the URL = "https://www1.nseindia.com/content/historical/DERIVATIVES/2018/OCT/fo23OCT2018bhav.csv.zip", the code returns a zip file of 557KB.

However, for URL = "https://www1.nseindia.com/content/historical/DERIVATIVES/2015/OCT/fo23OCT2015bhav.csv.zip", the code returns a zip file of 1KB. Upon trying to open this 1KB file, it throws an error "Windows Cannot Open the Folder. The Compressed (Zipped) Folder is invalid."

Apparently, when I try to download the file using a browser for URL = "https://www1.nseindia.com/content/historical/DERIVATIVES/2015/OCT/fo23OCT2015bhav.csv.zip"), it downloads a file of 341KB.

What is working using a browser is not working using the python code.

Here is the URL through which one can navigate and download the data:

https://www1.nseindia.com/products/content/derivatives/equities/archieve_fo.htm One has the select Report = Bhavcopy and Date as 23-10-2015

Am I missing something in the code?

Appreciate your help in this.

thanks,

import os
import time
import datetime
import requests
from zipfile import ZipFile
from random import choice

desktop_agents = ['Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.80 Safari/537.36 Edg/86.0.622.48']

def random_headers():
    return {'User-Agent': choice(desktop_agents),'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'}

today = datetime.date(2015,10,23)
#today = datetime.date(2018,10,23)

date=today.strftime('%d')
month=today.strftime('%b').upper()
year=today.strftime('%Y')

url="https://www1.nseindia.com/content/historical/DERIVATIVES/2015/OCT/fo23OCT2015bhav.csv.zip"
#url="https://www1.nseindia.com/content/historical/DERIVATIVES/2018/OCT/fo23OCT2018bhav.csv.zip"

#url = "https://www1.nseindia.com/content/historical/DERIVATIVES/" + year + "/" + month + "/" + "fo" + date + month + year + "bhav.csv.zip"
folder = "C:\\Temporary\\"
filename1 = folder + "fo" + date + month + year + "bhav.csv.zip"

print(url)

req = requests.get(url, allow_redirects=True, headers=random_headers())
file= open(filename1,'wb').write(req.content)

Image of Website Through Manual Navigation

0 Answers
Related