通过Python3.9执行REST API CALL时网关超时

yc0p9oo0  于 2023-03-31  发布在  Python
关注(0)|答案(1)|浏览(153)

我试图查询this site的历史价格交易量数据,但我的GET查询似乎超时了。我如何设置我的请求来绕过这个问题?
代码的设置方式是:
1.首先查询我想下载历史数据的股票(这里是EQUITASBNK)的登陆页面
1.我为收到的响应提取cookie
1.我使用这个cookie和修改的参数来(尝试提取)历史数据
步骤1 -〉状态200
步骤3 -〉代码挂起/卡住等待响应
这是我的代码:

class NSE(Exchange):
    def __init__(self):
        self.url_landing = "https://www.nseindia.com/get-quotes/equity?"
        self.url_quotes ="https://www.nseindia.com/api/historical/cm/equity?"

    def fetchbulkprices(self, ticker, fromdate, todate):
          sys.stderr.write("Querying Ticker = {} fromdate = {} todate {} \n".format(ticker, fromdate, todate))
          headers = {
            "authority": "www.nseindia.com",
            "method": "GET",
            "path": "/api/historical/cm/equity?symbol=" + ticker + "&series = [%22EQ%22]&from=" + fromdate + "&to="+ todate+ "&csv=true",
           "scheme": "https",
           "accept": "*/*",
           "accept-Encoding": "gzip, deflate, br",
           "accept-Language": "en-GB,en-US;q=0.9,en;q=0.8",
           "referer": "https://www.nseindia.com/get-quotes/equity?symbol="+ticker,
           "sec-ch-ua": "Google Chrome" + ";" + "v=""111"", ""Not(A:Brand""" + ";" + "v=""8""" + ",""Chromium""",
           "sec-ch-ua-mobile" : "?0",
           "sec-ch-ua-platform" : "Windows",
           "sec-fetch-dest": "empty",
           "sec-fetch-mode": "cors",
           "sec-fetch-site": "same-origin",
           "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36",
           "x -requested-with": "XMLHttpRequest"
           }
         session = requests.Session()
         params = {"symbol": ticker}
         response = requests.get(self.url_landing, params=params, headers=headers)
        cookies = response.cookies
        params = {"symbol": ticker, "series": "[%22EQ%22]", "fromDate": from date,"toDate": todate, "csv": True}
        response = session.get(self.url_quotes, params=params, headers=headers, cookies=cookies)
        if response.status_code == 200:
          sys.stderr.write("Queried successfully")

一些示例查询可以是(符号、起始日期、终止日期):

  1. AAVAS,2020年9月18日,2021年1月23日
  2. EQUITASBNK,18-09-2020,23-01-2021
  3. MASTEK,2020年9月18日,2021年1月23日
cwxwcias

cwxwcias1#

AAVAS,2020年9月18日,2021年1月23日
坏消息是,您无法使用所需的范围- nseindia API不允许您查询从现在起超过2年的数据:

{"error":true,"showMessage":"Please select date range not more than 2 Years."}

不过好消息是,你仍然可以使用普通的HTTP请求来获取历史数据。下面是一个简单的例子来获取AAVAS股票代码的历史数据:

import requests

HEADERS = {
    'accept': '*/*',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'en-GB,en;q=0.9',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                  'AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/111.0.0.0 Safari/537.36',
}

def find_ticker_historical_info(ticker: str, from_: str, to_: str) -> None:
    with requests.Session() as sess:
        # get a root page to get and save cookies necessary for other requests
        _ = sess.get('https://www.nseindia.com/', headers=HEADERS)
        
        # obtain hist data for a ticker
        r = sess.get(
            'https://www.nseindia.com/api/historical/cm/equity',
            headers=HEADERS,
            params={'symbol': ticker, 'from': from_, 'to': to_}
        )
        if not r.ok:
            print(r.status_code)
            print(r.text)
        else:
            print(r.json())

if __name__ == '__main__':
    find_ticker_historical_info('AAVAS', from_='24-01-2021', to_='23-01-2023')

相关问题