Python Requests get returns response code 401 for nse india website

Viewed 3290

I use this program to get the json data from https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY but since this morning it's not working as it returns <Response [401]>. The link loads fine on chrome though. Is there any way to fix this without using selenium ?

import json
import requests

headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, '
                         'like Gecko) '
                         'Chrome/80.0.3987.149 Safari/537.36',
           'accept-language': 'en,gu;q=0.9,hi;q=0.8', 'accept-encoding': 'gzip, deflate, br'}

res = requests.get("https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY", headers=headers)
print(res)

4 Answers

Try this:

import requests

baseurl = "https://www.nseindia.com/"
url = f"https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY"
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, '
                         'like Gecko) '
                         'Chrome/80.0.3987.149 Safari/537.36',
           'accept-language': 'en,gu;q=0.9,hi;q=0.8', 'accept-encoding': 'gzip, deflate, br'}
session = requests.Session()
request = session.get(baseurl, headers=headers, timeout=5)
cookies = dict(request.cookies)
response = session.get(url, headers=headers, timeout=5, cookies=cookies)
print(response.json())

To access the NSE (api's) site multiple times then set cookies in each subsequent requests:

response = session.get(url, headers=headers, timeout=5, cookies=cookies)

For performing and actions you first have to perform the login request call which will return you with session id .This session id will then be used in header section for next request to authenticate and return valid responce

Even I'm getting the same error from today morning. Earlier I used to face this issue only on servers hosted outside India. But now looks like they are trying to stop scraping altogether.

Really interested in finding a solution to this.

Also for the time being the old website still seems to be working. I know it's not a permanent solution but I'm using that as a stop gap until I find a more permanent solution.

I tried same thing with node js please use node js ide link here for online ide please review my code

const axios = require('axios');


let cookie;
let url_oc = "https://www.nseindia.com/option-chain"
let url = "https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY"
let headers = {
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
  'accept-language': 'en,gu;q=0.9,hi;q=0.8', 'accept-encoding': 'gzip, deflate, br'
}

const instance = axios.create({
  baseURL: url_oc,
  headers: headers,
  cookie: cookie ? cookie : ""
});



const getCookies = async () => {
  try {
    const response = await instance.get(url_oc);
    cookie = response.headers['set-cookie'].join();
  } catch (error) {
    if (error.response.status === 403) {
      console.log("getCookies =========> error.status === 403");
      await getCookies()
    } else {
      console.log("getCookies =========> error");
    }
  }
}


const getAPIData = async () => {
  try {
    if (cookie) {
      const response = await instance.get(url);
      console.log(response.data.records.timestamp);
    }

  } catch (error) {
    if (error.response && error.response.status === 401) {
      console.log("getAPIData =========> error.status === 401");
      if (!cookie) {
        console.log("getAPIData =========> cookie not found");
        await getCookies()
      }
      await getAPIData()
    } else {
      console.log("getAPIData =========> error");
    }
  }
}



(async () => {
  setInterval(async () => {
    await getCookies()
  }, 5000);

  setInterval(async () => {
    await getAPIData()
  }, 3000);
})()
Related