aeron7 / nsepython

The Unofficial Python Wrapper for NSEIndia API
https://unofficed.com/nse-python/
GNU General Public License v3.0
244 stars 117 forks source link

nsefetch(payload) for local throws Access Denied #45

Closed pratham-darooka closed 3 months ago

pratham-darooka commented 5 months ago

Tried nsefetch(“https://www.nseindia.com/api/equity-stockIndices?index=NIFTY%2050”) in a loop.

Output: b'<HTML><HEAD>\n<TITLE>Access Denied</TITLE>\n</HEAD><BODY>\n<H1>Access Denied</H1>\n \nYou don\'t have permission to access "http&#58;&#47;&#47;www&#46;nseindia&#46;com&#47;api&#47;equity&#45;stockIndices&#63;" on this server.<P>\nReference&#32;&#35;18&#46;5c01d517&#46;1715925139&#46;41f263\n<P>https&#58;&#47;&#47;errors&#46;edgesuite&#46;net&#47;18&#46;5c01d517&#46;1715925139&#46;41f263</P>\n</BODY>\n</HTML>\n’

mdtanveer commented 5 months ago

Can you please specify how fast you are looping in requests per minute?

pratham-darooka commented 5 months ago

@mdtanveer how fast should i be? I add a time.sleep(1)

buzzvolt commented 5 months ago

@pratham-darooka you are not going to get much help on this here..since it is not much of authors' problem, so he does not care .. look at https://github.com/aeron7/nsepython/pull/41 and https://github.com/aeron7/nsepython/pull/40 and https://github.com/aeron7/nsepython/pull/37

I would suggest you try this below alternate solution, that is, if you are simply trying to get the index constituents of Nifty 50

from io import StringIO
import pandas as pd
import requests

def get_nse_index_json_constituents(json_blob_url: str) -> list:
    nse_headers = {
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36',
        'accept-language': 'en,gu;q=0.9,hi;q=0.8',
        'accept-encoding': 'gzip, deflate, br'
    }
    response = requests.get(json_blob_url, headers=nse_headers)
    response_bytes = response.content
    response_str = response_bytes.decode('utf-8')
    return sorted(pd.read_json(StringIO(response_str)).symbol.tolist())

index_constituents = get_nse_index_json_constituents("https://iislliveblob.niftyindices.com/jsonfiles/HeatmapDetail/FinalHeatmapNIFTY%2050.json")

index_constituents will be a list for any further iterations you need.