Nse_results giving JSONDecodeError

Hi,

I am hitting an error while calling the Result API. I was earlier using the older method nse_events() in the older version of nsepython and it was working fine but in the newer version of nsepython (2.6), both nse_events() & nse_results() throw an error.

ipdb> nse_**results()**
*** requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
ipdb> nse_**results**("equities","Others")
*** requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
ipdb> nse_**events()**
*** requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Anyone else having the same issue?
1 Like

Hey Amit (@dexter),
Could you please advise on this.

It is very much system specific. As I use only server version I have no clue. Because it works for some people and does not work for some people.

What is your system configuration?

1 Like

For now, I rolled back to older version that is working on my other machine using below command, and I am able to get events data again. I was earlier on v2.6

pip install --force-reinstall -v "nsepython==0.0.972"
2 Likes

It means i have fucked something in middle. i willcheck

I thank you a lot. This is what happens in democracy. I had patched a code by someone in this forum and it worked for him and broke for 100 people lol. I am updating a new patch in nsepython. Install the latest version now.

1 Like

If You are having the problem, JSONDecodeEorr upgrade to the latest

pip3 install --upgrade nsepython

If the problem still persists, Let me know.

1 Like

The problem persists even after upgrading to the latest nsepython package

My code snippet copied from another site, Getting Indian Stock Prices Using Python | by Tejas Ekawade | Medium

from jugaad_data.nse import NSELive
n = NSELive()
tata_motors_price, mon100_etf = n.stock_quote(‘TATAMOTORS’)[‘priceInfo’][‘lastPrice’], n.stock_quote(‘MON100’)[‘priceInfo’][‘lastPrice’]

The error which I get is as follows


File “/home/viswayarasi/.conda/envs/dspy311_stocks/lib/python3.11/site-packages/jugaad_data/nse/live.py”, line 51, in get
return r.json()
^^^^^^^^
File “/home/viswayarasi/.conda/envs/dspy311_stocks/lib/python3.11/site-packages/requests/models.py”, line 978, in json
raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)


Hope this helps in resolving this issue

Same issue for me too. It seemed to work till last week (or last to last). But, is failing now.
Trying re-install if that solves problem. Will update post my observations.

@dexter : Hey Amit, This custom function seems to be failing. Could you please help on this or suggest an alternate function.


ipdb>                 OCDatajson = nse_optionchain_scrapper("SAIL")
*** requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
ipdb> nse_optionchain_scrapper("TCS")
*** requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
ipdb> nse_optionchain_scrapper("BANKNIFTY")
*** requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
ipdb> nse_optionchain_scrapper("NIFTY")
*** requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

And the package versions:

PS R:\Python\StockMarketAnalysis> pip install nsepython
Requirement already satisfied: nsepython in c:\users\rmittal\appdata\local\packages\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\localcache\local-packages\python311\site-packages (2.93)
Requirement already satisfied: requests in c:\users\rmittal\appdata\local\packages\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\localcache\local-packages\python311\site-packages (from nsepython) (2.32.3)
Requirement already satisfied: pandas in c:\users\rmittal\appdata\local\packages\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\localcache\local-packages\python311\site-packages (from nsepython) (2.2.3)
Requirement already satisfied: scipy in c:\users\rmittal\appdata\local\packages\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\localcache\local-packages\python311\site-packages (from nsepython) (1.14.1)
Requirement already satisfied: numpy>=1.23.2 in c:\users\rmittal\appdata\local\packages\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\localcache\local-packages\python311\site-packages (from pandas->nsepython) (2.1.2)
Requirement already satisfied: python-dateutil>=2.8.2 in c:\users\rmittal\appdata\local\packages\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\localcache\local-packages\python311\site-packages (from pandas->nsepython) (2.9.0.post0)
Requirement already satisfied: pytz>=2020.1 in c:\users\rmittal\appdata\local\packages\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\localcache\local-packages\python311\site-packages (from pandas->nsepython) (2024.2)
Requirement already satisfied: tzdata>=2022.7 in c:\users\rmittal\appdata\local\packages\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\localcache\local-packages\python311\site-packages (from pandas->nsepython) (2024.2)
Requirement already satisfied: charset-normalizer<4,>=2 in c:\users\rmittal\appdata\local\packages\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\localcache\local-packages\python311\site-packages (from requests->nsepython) (3.4.0)
Requirement already satisfied: idna<4,>=2.5 in c:\users\rmittal\appdata\local\packages\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\localcache\local-packages\python311\site-packages (from requests->nsepython) (3.10)
Requirement already satisfied: urllib3<3,>=1.21.1 in c:\users\rmittal\appdata\local\packages\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\localcache\local-packages\python311\site-packages (from requests->nsepython) (2.2.3)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\rmittal\appdata\local\packages\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\localcache\local-packages\python311\site-packages (from requests->nsepython) (2024.8.30)
Requirement already satisfied: six>=1.5 in c:\users\rmittal\appdata\local\packages\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\localcache\local-packages\python311\site-packages (from python-dateutil>=2.8.2->pandas->nsepython) (1.16.0)
PS R:\Python\StockMarketAnalysis> 

@Vish_Yarasi : Are you also using this same function and getting the error?

Hi, I am getting following error

‘.JSONDecodeError → … RequestsJSONDecodeError(e.msg, e.doc, e.pos) JSONDecodeError: Expecting value: line 1 column 1 (char 0)’

It was working fine but unable to access the data now. Can you suggest corrective measures?

Yes No change in code from my side. NSE is updating their site or endpoints or something. Let them finish. Then I will find remedy.

Sometimes they revert back their stuff and things work back automatically.

1 Like

HI… when we try to load Web Page some JavaScripts are preloaded and then the Json is displayed…

if we try to get from requests.get() it fetches bytes.

Just to update, it’s still not working, tried just now.

thanks for response…

I think NSE has updated its API with web-scrapping protection changes. I was reading it some time back about it. I tried reading the NSE API directly using python but it is throwing 401 each time (it used to work earlier).

@dexter : Could you please review if this function can be fixed.

I did a temp workaround myself and its remedied moment being. I will.
But in weekend.

Wait and watch for couple days because they can revert back their stuff as well meanwhile.

1 Like

@dexter Could you please share details on the workaround, so that folks can try it out themselves? I believe NSE has put restrictions on returning json object as part of response (API returns 200, but accessing json fails)

Selenium.
Trust me no one likes selenium.