[Python] Nse india Web scrapping not working, response showing error code 401

Discussão em 'Python' iniciado por Stack, Março 30, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to web scrap data from nse india website.I am attaching the code below.

    import requests

    import json

    import time

    def fun(sym):

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

    main_url = "https://www.nseindia.com/"

    response = requests.get(main_url, headers=headers)

    cookies = response.cookies

    url = "https://www.nseindia.com/api/option-chain-equities?symbol=" + sym

    session = requests.Session()

    request = session.get(url, headers=headers)

    response = session.get(url, headers=headers).text

    data = json.loads(response)

    print(data)


    if name == "main":

    sym = 'ABB'
    fun(sym)


    the print(data) is showing some error.

    The same code was working 2 weeks before but not working now. Can anyone let me know how to solve this issue? Thanks

    Continue reading...

Compartilhe esta Página