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.
Another error
With index_history
dexter
July 28, 2024, 2:27pm
10
Post the collab here with view access!
If needed will approve the access
dexter
July 28, 2024, 8:00pm
12
You did dumb stuff… It works just fine.
Understand the difference between nsepython and nsepythonserver.
They are different.
Hi
I tried several times its inconsistent while running
Index_history()
Sometimes it works like nothing happened and something it didn’t work.
And its not working from past 2 days
In continuation to previous Post
New error message
As of now its working fine just checked
dexter
August 12, 2024, 5:33am
16
Yes, just pass some exam and become NSE official and pay the server bill there timely.
Why don’t you do it, you hold better knowledge too
dexter
October 21, 2024, 9:46am
18
The tea stall owner infront of my house sells 250 cups a day on average.
10 INR a piece.
Estimating a profit of 5 INR it’s 250*5=1250
30 days = 37500
If you hire someone even with 10K you will get profit of 25K (talking roughly)
I am not even considering the sale of ciggerates and stuff.
Just open 4 shops. You earn more than NSE official. Why be naukaur of someone and do naukri
Jokes apart; They have changed compression settings in the server now which has broken everything. I am thinking to change the header or what
I just installed nsepython using pip and tried to get “NIFTY 50” using
df=index_history("NIFTY 50", "2024-10-20","2024-10-24")
I am getting the same old error
File ~/anaconda/envs/finance/lib/python3.12/site-packages/nsepython/rahu.py:748, in index_history(symbol, start_date, end_date)
746 data = {'cinfo': "{'name':'" + symbol + "','startDate':'" + start_date + "','endDate':'" + end_date + "','indexName':'" + symbol + "'}"}
747 payload = requests.post('https://niftyindices.com/Backpage.aspx/getHistoricaldatatabletoString', headers=niftyindices_headers, json=data).json()
--> 748 payload = json.loads(payload["d"])
749 payload=pd.DataFrame.from_records(payload)
750 return payload
KeyError: 'd'
I am unable to fix “KeyError”. Is there anything to do with json? Is the URL OK? Because I think it is not returning data.
Also I saw that you stressed on googlecolab. That uses nsepythonserver. If I wish to use nsepython in my laptop, Can’t I check “NIFTY 50”?
1 Like
dexter
October 27, 2024, 6:00pm
20
Update the package and retry
https://unofficed.com/nse-python/documentation/
. To upgrade the program to its latest version, run the following command:
pip install --upgrade nsepython