从HTML页面读取值-Nseindia [英] Reading value from HTML page - nseindia

查看:69
本文介绍了从HTML页面读取值-Nseindia的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从下面的网页中读取NIFTY 50的打开",高"和关闭"值. https://www1.nseindia.com/live_market/dynaContent/live_watch/live_index_watch.htm

I want to read "Open", "High" and "Close" value of NIFTY 50 from the below web page. https://www1.nseindia.com/live_market/dynaContent/live_watch/live_index_watch.htm

下面的代码之前是有效的.网页似乎有所变化,由于出现错误,我无法读取这些值.

The below code was working before. Looks like there is some change in the webpage, I am not able to read the values as I am getting below error.

nifty_50_row = table.find_all('tr')[2]          # get first row of prices
AttributeError: 'NoneType' object has no attribute 'find_all'

需要您的帮助来解决此问题.

Need your help to fix this issue.

我的代码如下:

url ='https://www1.nseindia.com/live_market/dynaContent/live_watch/live_index_watch.htm'

options = webdriver.ChromeOptions()
options.add_argument('headless') # disable Chrome browser GUI interface
driver = webdriver.Chrome(r'/usr/bin/chromedriver', options=options)
driver.get(url)
soup = bs4.BeautifulSoup(driver.page_source, 'html.parser')
table = soup.find('table', id='liveIndexWatch')

nifty_50_row = table.find_all('tr')[2]        

high_low = nifty_50_row.find_all('td')[3:7]   

h=high_low[1].text
c=high_low[3].text
l=high_low[2].text))
todays_open =high_low[0].text
todays_high = high_low[1].text
todays_low = high_low[2].text
prev_day_close = high_low[3].text

推荐答案

import requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0'
}

goal = ["open", "high", "previousClose"]


def main(url):
    r = requests.get(url, headers=headers).json()['data'][0]
    print(r)
    print("*" * 20)
    print([r[x] for x in goal])


main("https://www1.nseindia.com/live_market/dynaContent/live_watch/stock_watch/liveIndexWatchData.json")

输出:

{'timeVal': 'Apr 24, 2020 15:33:26', 'indexName': 'NIFTY 50', 'previousClose': '9,313.90', 'open': '9,163.90', 'high': '9,296.90', 'low': '9,141.30', 'last': '9,154.40', 'percChange': '-1.71', 'yearHigh': '12,430.50', 'yearLow': '7,511.10', 'indexOrder': '0.00'}
********************
['9,163.90', '9,296.90', '9,313.90']

这篇关于从HTML页面读取值-Nseindia的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆