1

我正在尝试使用下面的 python 代码从 NSE 网站获取数据,我得到了

[<span class="bold" id="orderBookTradeTMC">-</span>] 

安装的

[<span class="bold" id="orderBookTradeTMC">266</span>]

数据266未加载请咨询

import requests
import bs4
url="https://www.nseindia.com/get-quotes/equity?symbol=ITC"
headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) 
   Chrome/57.0.2987.133 Safari/537.36'}
response = requests.get(url, headers=headers)
soup=   bs4.BeautifulSoup(response.text,'lxml')
print(soup.find_all(id='orderBookTradeTMC'))
4

2 回答 2

0

数据通过 JavaScript 动态加载。您可以使用requests模块来模拟它。

例如:

import json
import requests


url="https://www.nseindia.com/get-quotes/equity?symbol=ITC"
api_url = 'https://www.nseindia.com/api/quote-equity?symbol={symbol}&section=trade_info'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:77.0) Gecko/20100101 Firefox/77.0',}

symbol = url.split('=')[-1]
data = requests.get(api_url.format(symbol=symbol), headers=headers).json()

# uncomment this to print all data:
# print(json.dumps(data, indent=4))

print('Total market capitalization:', data['marketDeptOrderBook']['tradeInfo']['totalMarketCap'])

印刷:

Total market capitalization: 24082488.32
于 2020-06-28T15:43:28.633 回答
0

尝试使用 Xpath 进行查找。我无法访问服务器,否则我想检查自己。

于 2020-06-28T15:52:22.167 回答