从www.NSEINDIA.com wesbite中刮取时,结果为空

2024-09-28 12:11:42 发布

您现在位置:Python中文网/ 问答频道 /正文

WWW1.NSEINDIA.COM正在努力刷新记录。NSE India最近推出了一个新的网站,采用了更新的技术。在这个网站上,我无法得到脚本的最后价格

我尝试了下面的代码来提取这些值,但它导致了HTML标记

import requests
from bs4 import BeautifulSoup

stockcode = "DISHTV"
print(stockcode)
stock_url = 'https://www.nseindia.com/get-quotes/equity?symbol=' + stockcode
print(stock_url)
headers = {'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'}
response = requests.get(stock_url, headers=headers)
print(response)


soup = BeautifulSoup(response.text, 'lxml')
data_array = soup.find(id="quoteLtp")
type(data_array)
print(data_array)

输出

DISHTV
https://www.nseindia.com/get-quotes/equity?symbol=DISHTV
<Response [200]>
<span id="quoteLtp"></span>

它应该给出13.50的结果是最后一个价格,但是我得到了标签作为输出enter image description here,我已经附上了HTML标签和值的屏幕截图

我希望有很多人会尝试并取得成功。我希望你能帮我检索信息

非常感谢

问候 雅娜


Tags: importurldataget网站responsehtmlstock
2条回答

页面是用javascript动态加载的,但是requests不支持它。 但是,您可以使用nsetools

pip install nsetools安装它

from pprint import pprint
from nsetools import Nse

stockcode = "DISHTV"
nse = Nse()
data = nse.get_quote(stockcode)

pprint(data)

输出:

  {'adhocMargin': None,
 'applicableMargin': 36.65,
 'averagePrice': 13.4,
 'basePrice': 13.55,
 'bcEndDate': None,
 'bcStartDate': None,
 'buyPrice1': None,
 'buyPrice2': None,
 'buyPrice3': None,
 'buyPrice4': None,
 'buyPrice5': None,
 'buyQuantity1': None,
 'buyQuantity2': None,
 'buyQuantity3': None,
 'buyQuantity4': None,
 'buyQuantity5': None,
 'change': '-0.05',
 'closePrice': 13.45,
 'cm_adj_high_dt': '21-NOV-19',
 'cm_adj_low_dt': '03-APR-20',
 'cm_ffm': 1238.24,
 'companyName': 'Dish TV India Limited',
 'css_status_desc': 'Listed',
 'dayHigh': 13.65,
 'dayLow': 13.25,
 'deliveryQuantity': 3983661.0,
 'deliveryToTradedQuantity': 77.06,
 'exDate': '05-NOV-18',
 'extremeLossMargin': 3.5,
 'faceValue': 1.0,
 'high52': 19.5,
 'indexVar': None,
 'isExDateFlag': False,
 'isinCode': 'INE836F01026',
 'lastPrice': 13.5,
 'low52': 3.9,
 'marketType': 'N',
 'ndEndDate': None,
 'ndStartDate': None,
 'open': 13.5,
 'pChange': '-0.37',
 'previousClose': 13.55,
 'priceBand': 10.0,
 'pricebandlower': 12.2,
 'pricebandupper': 14.9,
 'purpose': 'INTERIM DIVIDEND - RE 0.50 PER SHARE',
 'quantityTraded': 5169827.0,
 'recordDate': '06-NOV-18',
 'secDate': '09-Oct-2020 00:00:00',
 'securityVar': 33.15,
 'sellPrice1': 13.45,
 'sellPrice2': None,
 'sellPrice3': None,
 'sellPrice4': None,
 'sellPrice5': None,
 'sellQuantity1': 2399.0,
 'sellQuantity2': None,
 'sellQuantity3': None,
 'sellQuantity4': None,
 'sellQuantity5': None,
 'series': 'EQ',
 'surv_indicator': None,
 'symbol': 'DISHTV',
 'totalBuyQuantity': None,
 'totalSellQuantity': 2399.0,
 'totalTradedValue': 692.76,
 'totalTradedVolume': 5169827.0,
 'varMargin': 33.15}

要仅获取lastPrice的值,请执行以下操作:

print(data['lastPrice'])
>>> 13.5

即使使用nsetools,在新的nse网站上,您也会面临问题;如卡住等。请参阅github中nsetools的问题部分

更好的方法是在aliceblue或samco这样的经纪人那里开户,他们在那里有免费的API

相关问题 更多 >

    热门问题