Python Requests get returns response code 401 for nse india website
Asked Answered
P

6

7

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)

Professorship answered 20/9, 2020 at 16:43 Comment(3)
I am getting "Resource Not Found" when I try to open this in chrome.Oehsen
Works fine for me currently.Professorship
Same error for me. If I run the python script and then open the url in browser, it will show resource not found. Looks like NSE website is scanning for python script IPs and blocking them.Dissolve
P
25

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)

Professorship answered 24/9, 2020 at 10:1 Comment(5)
Thank you so much VarunS2002 I have been strugling to get this solution from last two day and here I found!Snowblink
If you're interested you can check my project out : github.com/VarunS2002/Python-NSE-Option-Chain-AnalyzerProfessorship
It means the world to me😍✌ Thank youNorm
I tried the same approach but in Java. It works some times but still starts failing with 401 response for repeated polls.Playreader
Can you help me with my question here #77228150?Playreader
U
1

To resolve the 401 Unauthorized error when fetching JSON data from https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY, use a requests.Session() to maintain session state and handle cookies. First, make a request to the base URL to establish the session, then request the data using the same session. Here's the code:

import requests

base_url = "https://www.nseindia.com/"
url = "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'
}

with requests.Session() as session:
    session.get(base_url, headers=headers)  # Establish session
    response = session.get(url, headers=headers)  # Fetch data
    if response.ok:
        data = response.json()
        print(data)
    else:
        print(f"Error: {response.status_code}")
Unlimber answered 22/3 at 22:46 Comment(0)
T
0

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

Timm answered 20/9, 2020 at 16:51 Comment(3)
Can you please share a code snippet of how that can be done in my program?Professorship
developers.onelogin.com/api-docs/1/login-page/… try referring this link. It shows how you can get the session token and the same session token can be used in successive api calls to get authenticate and interactTimm
NSE doesn't need a loginWolffish
C
0

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.

Crucifer answered 21/9, 2020 at 4:25 Comment(1)
Yeah the old website works fine for now but it can stop anytime so gotta be prepared.Professorship
R
0

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);
})()
Rescissory answered 1/4, 2021 at 12:45 Comment(1)
Abhi do you want code for node js or python. I was tested above while that time it was workingRescissory
N
0

Got the same error with Java. Solution is

  • first make connection to https://nseindia.com.
  • And then read the cookie returned by the server.
  • Add the same cookie in your request.

Depending on which implementation you are using to make ssl calls, you will have to find appropriate methods to get and set cookies. Dont know why NSE site has to look for a cookie when the data is publicly enabled. Some extra technology applied by nse for sure!.

Nevertheless answered 9/1, 2023 at 13:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.