简体   繁体   English

Python 请求 401 错误但 url 在浏览器中打开

[英]Python requests 401 error but url opens in browser

I am trying to pull the json from this location - https://www.nseindia.com/api/option-chain-indices?symbol=BANKNIFTY我试图从这个位置拉出 json - https://www.nseindia.com/api/option-chain-indices?symbol=BANKNIFTY

This opens fine in my browser, but using requests in python throws a 401 permission error.这在我的浏览器中打开正常,但使用 python 中的请求会引发 401 权限错误。 I have tried adding headers with different arguments, but to no avail.我尝试添加具有不同 arguments 的标头,但无济于事。 Interestingly, the json on this page does not open in the browser as well until https://www.nseindia.com is opened separately.有趣的是,这个页面上的 json 在浏览器中也没有打开,直到https://www.nseindia.com单独打开。 I believe it requires some kind of authentication, but surprised it works in the browser without any.我相信它需要某种身份验证,但令人惊讶的是它无需任何身份验证即可在浏览器中运行。

Is there a way to extract the information from this url?有没有办法从这个 url 中提取信息? Any help is much appreciated.任何帮助深表感谢。

Here is my implementation -这是我的实现-

import requests

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

# This throws a 401 response error
page = requests.get(url, headers={"User-Agent": "Mozilla/5.0"})

# This throws a 'Connection aborted' error
page = requests.get(url, headers={"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 GTB7.1 (.NET CLR 3.5.30729)"})

To get the correct data, first load cookies from other URL with requests.get() and then do Ajax request to load the JSON:要获取正确的数据,首先使用requests.get()从其他 URL 加载 cookies,然后执行 Ajax 请求加载 JSON:

import json
import requests
from bs4 import BeautifulSoup


headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0'}
url = 'https://www.nseindia.com/api/option-chain-indices?symbol=BANKNIFTY'

with requests.session() as s:

    # load cookies:
    s.get('https://www.nseindia.com/get-quotes/derivatives?symbol=BANKNIFTY', headers=headers)

    # get data:
    data = s.get(url, headers=headers).json()

    # print data to screen:
    print(json.dumps(data, indent=4))

Prints:印刷:

{
    "records": {
        "expiryDates": [
            "03-Sep-2020",
            "10-Sep-2020",
            "17-Sep-2020",
            "24-Sep-2020",
            "01-Oct-2020",
            "08-Oct-2020",
            "15-Oct-2020",
            "22-Oct-2020",
            "29-Oct-2020",
            "26-Nov-2020"
        ],
        "data": [
            {
                "strikePrice": 18100,
                "expiryDate": "03-Sep-2020",
                "CE": {
                    "strikePrice": 18100,
                    "expiryDate": "03-Sep-2020",
                    "underlying": "BANKNIFTY",
                    "identifier": "OPTIDXBANKNIFTY03-09-2020CE18100.00",
                    "openInterest": 1,
                    "changeinOpenInterest": 1,
                    "pchangeinOpenInterest": 0,
                    "totalTradedVolume": 2,
                    "impliedVolatility": 95.51,
                    "lastPrice": 6523.6,
                    "change": 2850.1000000000004,
                    "pChange": 77.58540901048048,
                    "totalBuyQuantity": 2925,
                    "totalSellQuantity": 2800,
                    "bidQty": 25,
                    "bidprice": 6523.6,
                    "askQty": 25,
                    "askPrice": 6570.3,
                    "underlyingValue": 24523.8
                },
                "PE": {
                    "strikePrice": 18100,
                    "expiryDate": "03-Sep-2020",
                    "underlying": "BANKNIFTY",

...and so on.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM