2

I'm new to python and struggling with below.

The website page URL is https://www.nseindia.com/market-data/equity-derivatives-watch and when we select "Nifty 50 Futures" and upon inspect, we get the api URL as https://www.nseindia.com/api/liveEquity-derivatives?index=nse50_fut. Now the issue is this json opens up on browser but from python it does not open and gives JSONDecodeError error. I have included right header but still it fails.

One more observation is that when i load this api directly in browser, the python code gets json data once but it does not work there after. One thing i noticed is that a new cookies is set on every page refresh.

Can anyone help me where I'm missing.

Code:

    header = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36',
              "accept-language": "en-US,en;q=0.9", "accept-encoding": "gzip, deflate, br", "accept": "*/*"}

    URL = "https://www.nseindia.com/api/liveEquity-derivatives?index=nse50_fut"
    fut_json = requests.get(URL, headers = header).json()
    print(fut_json)


  File "C:\ProgramData\Anaconda3\lib\site-packages\simplejson\decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())

JSONDecodeError: Expecting value

1
  • don't parse directory using .json() , check .status_code. If it is 200 then only parse. In this case trying your code gives 401 which means unauthorized. So you need to authorize yourself, then only will get the json Commented Feb 14, 2021 at 10:38

1 Answer 1

1

You need cookies to get the response as JSON, as without them you get Resource not found.

Here's how:

import requests

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36',
}

with requests.Session() as s:
    r = s.get("https://www.nseindia.com", headers=headers)
    api_url = "https://www.nseindia.com/api/liveEquity-derivatives?index=nse50_fut"
    response = s.get(api_url, headers=headers).json()
    print(response["marketStatus"]["marketStatusMessage"])

Output:

Market is Closed
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.