Home > Blockchain >  JSONDecodeError occurs sometimes and sometimes not
JSONDecodeError occurs sometimes and sometimes not

Time:04-30

I am trying to fetch the "Underlying Index" value from https://www.nseindia.com/api/option-chain-indices?symbol=BANKNIFTY

url = "https://www.nseindia.com/api/option-chain-indices?symbol=BANKNIFTY"

bnf_text = requests.get(url, headers={"User-Agent": "Mozilla/5.0 (X11; CrOS x86_64 12871.102.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.141 Safari/537.36"})

underlying_value = json.loads(bnf_text.text)

underlying_value = underlying_value['records']['underlyingValue']

print(underlying_value)

Sometimes, it fetches the value. In other cases, it gives me:

raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)"

How can I avoid this error from occurring?

CodePudding user response:

As is often the case, someone made a library to do exactly what you're trying to do.

https://nsetools.readthedocs.io/en/latest/introduction.html

pip install nsetools

from nsetools import Nse
nse = Nse()
quote = nse.get_index_quote("NIFTY BANK")
print(quote['lastPrice'])

Output:

36088.15

CodePudding user response:

You must look at the response status code before trying to parse the JSON.

A HTTP 200 response indicates success, most other codes indicate an error.

import requests 

def get_option_chain_indices(symbol):
    response = requests.get('https://www.nseindia.com/api/option-chain-indices', {
        'symbol': symbol
    }, headers={
        'User-Agent': 'Mozilla/5.0 (X11; CrOS x86_64 12871.102.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.141 Safari/537.36'
    })
    if response.status_code == 200:
        return response.json()
    else:
        print("Response {response.status_code} {response.reason}: {response.text}")
        # maybe sleep(1) and try again, depending on status code
        # or maybe raise an error to inform the caller, and let the caller decide

data = get_option_chain_indices('BANKNIFTY')
if data:
    print(['records']['underlyingValue'])
  • Related