Error while importing stock data from nsepy

Hi All,

I am studing the course namely 'Algorithemic trading for beginners'. I am a newbie in this area. While trying to extract stock data from nsepy using the below code , I am getting an error which says 

TooManyRedirects: Exceeded 30 redirects.
from nsepy import get_history
from datetime import date



data = get_history(symbol="SBIN", start=date(2015,1,1), end=date(2015,1,31))

Following is the error I am getting
TooManyRedirects                          Traceback (most recent call last)
Cell In[4], line 6
      1 from nsepy import get_history
      2 from datetime import date
----> 6 data = get_history(symbol="SBIN", start=date(2015,1,1), end=date(2015,1,31))

File ~\anaconda3\envs\quantra_py\lib\site-packages\nsepy\history.py:138, in get_history(symbol, start, end, index, futures, option_type, expiry_date, strike_price, series)
    136     return pd.concat((t1.result, t2.result))
    137 else:
--> 138     return get_history_quanta(**kwargs)

File ~\anaconda3\envs\quantra_py\lib\site-packages\nsepy\history.py:143, in get_history_quanta(**kwargs)
    141 def get_history_quanta(**kwargs):
    142     url, params, schema, headers, scaling = validate_params(**kwargs)
--> 143     df = url_to_df(url=url,
    144                    params=params,
    145                    schema=schema,
    146                    headers=headers, scaling=scaling)
    147     return df

File ~\anaconda3\envs\quantra_py\lib\site-packages\nsepy\history.py:151, in url_to_df(url, params, schema, headers, scaling)
    150 def url_to_df(url, params, schema, headers, scaling={}):
--> 151     resp = url(**params)
    152     bs = BeautifulSoup(resp.text, 'lxml')
    153     tp = ParseTables(soup=bs,
    154                      schema=schema,
    155                      headers=headers, index="Date")

File ~\anaconda3\envs\quantra_py\lib\site-packages\nsepy\commons.py:167, in URLFetch.__call__(self, *args, **kwargs)
    165 url = self.url%(args)
    166 if self.method == 'get':
--> 167     return self.session.get(url, params=kwargs, proxies = self.proxy )
    168 elif self.method == 'post':
    169     if self.json:

File ~\anaconda3\envs\quantra_py\lib\site-packages\requests\sessions.py:600, in Session.get(self, url, **kwargs)
    592 r"""Sends a GET request. Returns :class:`Response` object.
    593 
    594 :param url: URL for the new :class:`Request` object.
    595 :param \*\*kwargs: Optional arguments that ``request`` takes.
    596 :rtype: requests.Response
    597 """
    599 kwargs.setdefault("allow_redirects", True)
--> 600 return self.request("GET", url, **kwargs)

File ~\anaconda3\envs\quantra_py\lib\site-packages\requests\sessions.py:587, in Session.request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
    582 send_kwargs = {
    583     "timeout": timeout,
    584     "allow_redirects": allow_redirects,
    585 }
    586 send_kwargs.update(settings)
--> 587 resp = self.send(prep, **send_kwargs)
    589 return resp

File ~\anaconda3\envs\quantra_py\lib\site-packages\requests\sessions.py:723, in Session.send(self, request, **kwargs)
    720 if allow_redirects:
    721     # Redirect resolving generator.
    722     gen = self.resolve_redirects(r, request, **kwargs)
--> 723     history = [resp for resp in gen]
    724 else:
    725     history = []

File ~\anaconda3\envs\quantra_py\lib\site-packages\requests\sessions.py:723, in <listcomp>(.0)
    720 if allow_redirects:
    721     # Redirect resolving generator.
    722     gen = self.resolve_redirects(r, request, **kwargs)
--> 723     history = [resp for resp in gen]
    724 else:
    725     history = []

File ~\anaconda3\envs\quantra_py\lib\site-packages\requests\sessions.py:191, in SessionRedirectMixin.resolve_redirects(self, resp, req, stream, timeout, verify, cert, proxies, yield_requests, **adapter_kwargs)
    188     resp.raw.read(decode_content=False)
    190 if len(resp.history) >= self.max_redirects:
--> 191     raise TooManyRedirects(
    192         f"Exceeded {self.max_redirects} redirects.", response=resp
    193     )
    195 # Release the connection back into the pool.
    196 resp.close()

TooManyRedirects: Exceeded 30 redirects.

Hi Jobin,



You can fetch the stock data from Yahoo Finance using the following code:

 

import yfinance as yf

# Define the stock symbol (SBIN.NS in this case)
stock_symbol = 'SBIN.NS'

# Define the start and end dates
start_date = '2015-01-01'
end_date = '2015-01-31'

# Retrieve stock data using yfinance
data = yf.download(stock_symbol, start=start_date, end=end_date)

# Display the data
data.head()

Hope this helps!