KeyError Traceback (most recent call last)
in <cell line: 4>()
2 start_date = “01-Mar-2011”
3 end_date = “07-Jun-2021”
----> 4 print(index_history(symbol,start_date,end_date))
/usr/local/lib/python3.10/dist-packages/nsepython/rahu.py in index_history(symbol, start_date, end_date)
747 payload = requests.post(‘https://niftyindices.com/Backpage.aspx/getHistoricaldatatabletoString ’, headers=niftyindices_headers, data=data).json()
748 # print(payload)
→ 749 payload = json.loads(payload[“d”])
750 payload=pd.DataFrame.from_records(payload)
751 return payload
KeyError: ‘d’
2 Likes
There is a change in NIFTY Indicies website. So We need to update the wrapper accordingly.
Tried to see once. Your collab said I don’t have access. So I forgot afterwards. They may have changed something in middle.
Use this for meanwhile.
import requests
headers = {
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Language': 'en-US,en;q=0.9',
'Connection': 'keep-alive',
'Content-Type': 'application/json; charset=UTF-8',
'DNT': '1',
'Origin': 'https://niftyindices.com',
'Referer': 'https://niftyindices.com/reports/historical-data',
…
Refer to this temporary workaround. Within 24 hours a new Wrapper update is expected. It will be solved in that update.
1 Like
Hello there,
See in my opinion It seems that there is an issue with accessing the ‘d’ key in the JSON response. It is likely that the JSON structure returned by the API has changed or there might be a typo in the key.
Also, Double check the API documentation or the response structure to ensure that you are accessing the correct key.
Hope this could help in resolving your query.
As said earlier, There is a change in NIFTY Indicies website.
It will be fixed in this update.
Waiting for the update and thanks for sparing a time mate.
waiting for the new api dear
dexter
July 14, 2024, 9:54pm
7
Starting on it.
I did on your google collab only. It was so easy then… Its still easy now.
They have changed the JSON structure.
Why this takes time… and attention… normal stuff bro
def index_history(symbol,start_date,end_date):
data = {'cinfo': "{'name':'" + symbol + "','startDate':'" + start_date + "','endDate':'" + end_date + "','indexName':'" + symbol + "'}"}
payload = requests.post('https://niftyindices.com/Backpage.aspx/getHistoricaldatatabletoString', headers=niftyindices_headers, json=data).json()
payload = json.loads(payload["d"])
payload=pd.DataFrame.from_records(payload)
return payload
def index_pe_pb_div(symbol,start_date,end_date):
data = {'cinfo': "{'name':'" + symbol + "','startDate':'" + start_date + "','endDate':'" + end_date + "','indexName':'" + symbol + "'}"}
payload = requests.post('https://niftyindices.com/Backpage.aspx/getpepbHistoricaldataDBtoString', headers=niftyindices_headers, json=data).json()
payload = json.loads(payload["d"])
payload=pd.DataFrame.from_records(payload)
return payload
def index_total_returns(symbol,start_date,end_date):
data = {'cinfo': "{'name':'" + symbol + "','startDate':'" + start_date + "','endDate':'" + end_date + "','indexName':'" + symbol + "'}"}
payload = requests.post('https://niftyindices.com/Backpage.aspx/getTotalReturnIndexString', headers=niftyindices_headers, json=data).json()
payload = json.loads(payload["d"])
payload=pd.DataFrame.from_records(payload)
return payload
Three functions will be fixed.
dexter
July 14, 2024, 10:19pm
8
PS - Update NSEPython. Refer https://unofficed.com/nse-python/documentation/#getting-started on how to. or Google out. It should work fine.