You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Python网络爬虫请求失效,返回Response [401]错误求助

解决NSE India API请求401未授权错误的方案

Hey there, let's figure out how to fix that 401 Unauthorized error you're hitting with your NSE India API scrape. I've run into similar issues with their endpoints before, so here are actionable steps to resolve this:

  • Update your User-Agent header
    The Chrome 84 User-Agent in your code is pretty outdated—NSE's servers likely block older browser versions to prevent automated requests. Swap it for a modern, widely used UA string. For example:

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

    You can check your current browser's UA string (via about:version in Chrome) to use an exact match if you prefer.

  • Add additional required request headers
    Many websites, including NSE, check for headers like Referer to verify the request is coming from their official site. Update your headers to include these:

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
        'Referer': 'https://www.nseindia.com/',
        'Accept-Language': 'en-US,en;q=0.9'
    }
    

    The Referer header tells the server your request originated from NSE's homepage, which reduces the chance of being flagged as a bot.

  • Use a session to maintain cookies
    NSE's APIs often require valid session cookies that are set when you first visit their homepage. Use requests.Session() to persist these cookies across requests:

    import requests
    
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
        'Referer': 'https://www.nseindia.com/'
    }
    
    # First, visit the homepage to get session cookies
    session = requests.Session()
    session.get('https://www.nseindia.com/', headers=headers)
    
    # Now make your API request using the session
    url = 'https://www.nseindia.com/api/chart-databyindex?index=ACCEQN'
    response = session.get(url, headers=headers)
    
    # Check if the request succeeded
    if response.status_code == 200:
        data = response.json()
        prices = data['grapthData']
        print(prices)
    else:
        print(f"Request failed with status code: {response.status_code}")
    

    This ensures your API request carries the same session cookies as a regular browser visit, which is often required to bypass 401 checks.

  • Verify the API endpoint is still valid
    It's possible NSE has updated their API path. To confirm, open your browser's DevTools (F12), navigate to the ACCEQN index page on NSE's website, and check the "Network" tab for XHR requests. Look for the chart data request—if the URL has changed, update your code to match the new endpoint.

Give these steps a try in order—most of the time, updating the User-Agent and using a session with cookies will resolve the 401 error for NSE's APIs.

内容的提问来源于stack exchange,提问作者vpnkumar

火山引擎 最新活动