python-3.x 刮除NSE页面的技巧是什么?

x33g5p2x  于 2023-05-02  发布在  Python
关注(0)|答案(1)|浏览(103)

基本上,我试图这个链接https://www.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?segmentLink=17&instrument=OPTIDX&symbol=BANKNIFTY
下面的代码是解决方案-

import requests
headers = {
    'Connection': 'keep-alive',
    '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.79 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',
}

response = requests.get('https://www1.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?segmentLink=17&instrument=OPTIDX&symbol=NIFTY', headers=headers, verify=True,timeout=(5, 14))
print(response.content)

它在我的笔记本电脑上工作完美,但它不能在谷歌Collab或Heroku服务器或数字海洋以及在运动托管上工作。

这里的catch是什么?

yxyvkwin

yxyvkwin1#

在各种环境中测试后,NSE阻止了Python请求以及大多数知名云服务器对该网站的正常访问。
但是,CURL在DigitalOcean班加罗尔和Amazon AWS孟买服务器上工作。但是如果你把CURL数据转换成Python请求,那就被阻塞了。
这是一个可以在云服务器上工作的Python解决方案。它看起来很蹩脚,但我用它没有深入挖掘-

import subprocess
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))

subprocess.Popen('curl "https://www.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  -o maxpain.txt', shell=True)

f=open("maxpain.txt","r")
var=f.read()
print(var)

它基本上运行curl函数,并将输出发送到一个文件,然后读回文件。就是这样

**更新-〉**我添加了一个GitHub库。上面的书面答案基本上是下面列出的curl方法

使用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)

如果你想要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)

相关问题