【问题标题】:The result empty when scrape from www.NSEINDIA.com wesbite从 www.NSEINDIA.com 网站抓取时结果为空
【发布时间】:2020-10-09 17:14:51
【问题描述】:

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的结果是最后的价格,但我得到的标签是output,我附上了HTML标签和值的截图

我希望有很多人会尝试并获得成功的结果。希望您能帮我找回资料。

在此先感谢

问候 贾娜

【问题讨论】:

    标签: python python-3.x python-requests


    【解决方案1】:

    页面使用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
    

    【讨论】:

      【解决方案2】:

      即使使用 nsetools,在新的 nse 网站中,您也会遇到问题;比如卡住等。参考github中nsetools的issues部分。

      在 aliceblue 或 samco 等经纪商处开设账户的更好方法,他们有免费的 API。

      【讨论】:

        猜你喜欢
        • 2021-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-08
        • 2019-03-31
        • 1970-01-01
        • 1970-01-01
        • 2019-04-12
        相关资源
        最近更新 更多