Getting an error


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.

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

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.

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

Post the collab here with view access!

If needed will approve the access


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

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

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 :stuck_out_tongue: :smiling_face_with_three_hearts:

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

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