此 python 代码在本地计算机上运行,但未在
和许多其他 VPS。它显示不同的错误不同的时间。
import requests
headers = {
'authority': 'beta.nseindia.com',
'cache-control': 'max-age=0',
'dnt': '1',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36',
'sec-fetch-user': '?1',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'sec-fetch-site': 'none',
'sec-fetch-mode': 'navigate',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9,hi;q=0.8',
}
params = (
('symbol', 'BANKNIFTY'),
)
response = requests.get('https://beta.nseindia.com/api/quote-derivative', headers=headers, params=params)
#NB. Original query string below. It seems impossible to parse and
#reproduce query strings 100% accurately so the one below is given
#in case the reproduced version is not "correct".
# response = requests.get('https://beta.nseindia.com/api/quote-derivative?symbol=BANKNIFTY', headers=headers)
Run Code Online (Sandbox Code Playgroud)
上面这段代码有什么错误吗?我缺少什么?我在隐身模式下从 Chrome 开发者工具>网络复制标题数据,使用https://curl.trillworks.com/站点从 curl 命令生成 python 代码。
但是 curl 命令工作正常并提供良好的输出 -
curl "https://beta.nseindia.com/api/quote-derivative?symbol=BANKNIFTY" -H "authority: beta.nseindia.com" -H "cache-control: max-age=0" -H "dnt: 1" -H "upgrade-insecure-requests: 1" -H "user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36" -H "sec-fetch-user: ?1" -H "accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" -H "sec-fetch-site: none" -H "sec-fetch-mode: navigate" -H "accept-encoding: gzip, deflate, br" -H "accept-language: en-US,en;q=0.9,hi;q=0.8" --compressed
Run Code Online (Sandbox Code Playgroud)
curl 命令如何工作,但从 curl 命令生成的 python 却没有?
小智 7
有两件事需要注意。
__request_headers = {
'Host':'www.nseindia.com',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language':'en-US,en;q=0.5',
'Accept-Encoding':'gzip, deflate, br',
'DNT':'1',
'Connection':'keep-alive',
'Upgrade-Insecure-Requests':'1',
'Pragma':'no-cache',
'Cache-Control':'no-cache',
}
Run Code Online (Sandbox Code Playgroud)
'nsit',
'nseappid',
'ak_bmsc'
Run Code Online (Sandbox Code Playgroud)
这些是根据正在使用的功能从 nse 设置的。这个例子:最大赢家/输家。我试图获取最大赢家和输家列表,其中请求在没有这些 cookie 的情况下被阻止。
try:
nse_url = 'https://www.nseindia.com/market-data/top-gainers-loosers'
url = 'https://www.nseindia.com/api/live-analysis-variations?index=gainers'
resp = requests.get(url=nse_url, headers=__request_headers)
if resp.ok:
req_cookies = dict(nsit=resp.cookies['nsit'], nseappid=resp.cookies['nseappid'], ak_bmsc=resp.cookies['ak_bmsc'])
tresp = requests.get(url=url, headers=__request_headers, cookies=req_cookies)
result = tresp.json()
res_data = result["NIFTY"]["data"] if "NIFTY" in result and "data" in result["NIFTY"] else []
if res_data != None and len(res_data) > 0:
__top_list = res_data
except OSError as err:
logger.error('Unable to fetch data')
Run Code Online (Sandbox Code Playgroud)
另一件需要注意的事情是,NSE 会阻止来自大多数云虚拟机(如 AWS、GCP)的这些请求。我可以从个人 Windows 机器获取它,但不能从 AWS 或 GCP 获取。
使用此处记录的 nsefetch() 函数https://unofficed.com/nse-python/documentation/nsefetch/
如果你想要python-requests方法
from nsepython import *
payload= nsefetch('https://www.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?segmentLink=17&instrument=OPTIDX&symbol=BANKNIFTY')
print(payload)
Run Code Online (Sandbox Code Playgroud)
这适用于所有台式机和笔记本电脑。
如果你想要curl方法
from nsepythonserver import *
payload= nsefetch('https://www.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?segmentLink=17&instrument=OPTIDX&symbol=BANKNIFTY')
print(payload)
Run Code Online (Sandbox Code Playgroud)
这适用于所有服务器。就像Linux服务器一样。
| 归档时间: |
|
| 查看次数: |
1758 次 |
| 最近记录: |