首页 > 解决方案 > 从 www.NSEINDIA.com wesbite 抓取时结果为空

问题描述

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的结果是最后的价格,但我将标签作为输出在此处输入图像描述,我附上了 HTML 标签和值的屏幕截图

我希望有很多人会尝试并获得成功的结果。我希望你能帮助我检索信息。

提前谢谢了

问候贾娜

标签: pythonpython-3.xpython-requests

解决方案


该页面使用 动态加载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

推荐阅读