vsjha18 / nsetools

Realtime Data From National Stock Exchange (India)
http://nsetools.readthedocs.io
MIT License
817 stars 422 forks source link

Cant download the bhavcopy file #110

Open 12345k opened 3 years ago

12345k commented 3 years ago

Getting 403 (forbidden error)

from nsetools import Nse
nse=Nse()
nse.download_bhavcopy("01-01-2021")

HTTPError Traceback (most recent call last)

in ----> 1 nse.download_bhavcopy("01-01-2021") ~/.virtualenvs/zerodha/lib/python3.8/site-packages/nsetools/nse.py in download_bhavcopy(self, d) 436 filename = self.get_bhavcopy_filename(d) 437 # response = requests.get(url, headers=self.headers) --> 438 response = self.opener.open(Request(url, None, self.headers)) 439 zip_file_handle = io.BytesIO(response.read()) 440 zf = zipfile.ZipFile(zip_file_handle) /usr/lib/python3.8/urllib/request.py in open(self, fullurl, data, timeout) 529 for processor in self.process_response.get(protocol, []): 530 meth = getattr(processor, meth_name) --> 531 response = meth(req, response) 532 533 return response /usr/lib/python3.8/urllib/request.py in http_response(self, request, response) 638 # request was successfully received, understood, and accepted. 639 if not (200 <= code < 300): --> 640 response = self.parent.error( 641 'http', request, response, code, msg, hdrs) 642 /usr/lib/python3.8/urllib/request.py in error(self, proto, *args) 567 if http_err: 568 args = (dict, 'default', 'http_error_default') + orig_args --> 569 return self._call_chain(*args) 570 571 # XXX probably also want an abstract factory that knows when it makes /usr/lib/python3.8/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args) 500 for handler in handlers: 501 func = getattr(handler, meth_name) --> 502 result = func(*args) 503 if result is not None: 504 return result /usr/lib/python3.8/urllib/request.py in http_error_default(self, req, fp, code, msg, hdrs) 647 class HTTPDefaultErrorHandler(BaseHandler): 648 def http_error_default(self, req, fp, code, msg, hdrs): --> 649 raise HTTPError(req.full_url, code, msg, hdrs, fp) 650 651 class HTTPRedirectHandler(BaseHandler): HTTPError: HTTP Error 403: Forbidden
half-nirvana commented 3 years ago

You can try jugaad-data library https://github.com/jugaad-py/jugaad-data

codebraker commented 3 years ago

Getting 403 (forbidden error)

from nsetools import Nse nse=Nse() nse.download_bhavcopy("01-01-2021")

HTTPError Traceback (most recent call last) in ----> 1 nse.download_bhavcopy("01-01-2021")

~/.virtualenvs/zerodha/lib/python3.8/site-packages/nsetools/nse.py in download_bhavcopy(self, d) 436 filename = self.get_bhavcopy_filename(d) 437 # response = requests.get(url, headers=self.headers) --> 438 response = self.opener.open(Request(url, None, self.headers)) 439 zip_file_handle = io.BytesIO(response.read()) 440 zf = zipfile.ZipFile(zip_file_handle)

/usr/lib/python3.8/urllib/request.py in open(self, fullurl, data, timeout) 529 for processor in self.process_response.get(protocol, []): 530 meth = getattr(processor, meth_name) --> 531 response = meth(req, response) 532 533 return response

/usr/lib/python3.8/urllib/request.py in http_response(self, request, response) 638 # request was successfully received, understood, and accepted. 639 if not (200 <= code < 300): --> 640 response = self.parent.error( 641 'http', request, response, code, msg, hdrs) 642

/usr/lib/python3.8/urllib/request.py in error(self, proto, args) 567 if http_err: 568 args = (dict, 'default', 'http_error_default') + orig_args --> 569 return self._call_chain(args) 570 571 # XXX probably also want an abstract factory that knows when it makes

/usr/lib/python3.8/urllib/request.py in _call_chain(self, chain, kind, meth_name, args) 500 for handler in handlers: 501 func = getattr(handler, meth_name) --> 502 result = func(args) 503 if result is not None: 504 return result

/usr/lib/python3.8/urllib/request.py in http_error_default(self, req, fp, code, msg, hdrs) 647 class HTTPDefaultErrorHandler(BaseHandler): 648 def http_error_default(self, req, fp, code, msg, hdrs): --> 649 raise HTTPError(req.full_url, code, msg, hdrs, fp) 650 651 class HTTPRedirectHandler(BaseHandler):

HTTPError: HTTP Error 403: Forbidden

Any updates here? It seems that NSE website is allowing to download Bhavcopy report from within the browser. But the same is not permitted from script. There seems some javascript based enabling from web browser.

Any help/comments would be appreciated.

ashishdalvi3 commented 3 years ago

Issue exists only if you download 2021 data, for 2020 data able to download bhavcopy file.

ashishdalvi3 commented 3 years ago

Fix is to add Referer under nse_headers function @vsjha18

            'Referer': 'https://www1.nseindia.com/products/content/equities/equities/archieve_eq.htm'
rajvardhan21 commented 3 years ago

Can we download multiple stock historical data at once and update it daily? For example,can we download all FnO stock data (mainly OHLC) and download in Excel for last one one year or so? If yes, how??