寻找从Python3读取HTML文件中的表格内容的方法

时间:2018-12-28 00:47:37

标签: html python-3.x

我正在寻找从下面的Python3网页中读取NIFTY 50低和高值的方法。

https://www.nseindia.com/live_market/dynaContent/live_watch/live_index_watch.htm

我尝试使用bs4和Selenium Webdrivers读取这些值。您能告诉我如何阅读它们吗?

关于, 拉姆

1 个答案:

答案 0 :(得分:0)

没有看到您使用Selenium和bs4编写的代码,我们不知道为什么它对您不起作用。但是这段代码似乎有效:

import bs4
import requests
from selenium import webdriver

url = 'https://www.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'path_to_chromedriver.exe', options=options)
driver.get(url)

soup = bs4.BeautifulSoup(driver.page_source, 'html.parser')

table = soup.find('table', id='liveIndexWatch') # get the first table
nifty_50_row = table.find_all('tr')[2]          # get first row of prices

high_low = nifty_50_row.find_all('td')[4:6]     # get 'high'/'low' columns

# format output
print('NIFTY 50 High: {h} Low: {l}'.format(h=high_low[0].text, l=high_low[1].text))

打印:

NIFTY 50 High: 10,834.20 Low: 10,764.45         # current values