【问题标题】:Reading value from HTML page - nseindia从 HTML 页面读取值 - nseindia
【发布时间】:2020-08-08 20:32:50
【问题描述】:

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

下面的代码以前可以工作。看起来网页有一些变化,我无法读取这些值,因为我遇到了错误。

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

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

我的代码如下:

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

【问题讨论】:

    标签: python-3.x beautifulsoup


    【解决方案1】:
    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']
    

    【讨论】:

      猜你喜欢
      • 2013-09-25
      • 2013-11-09
      • 2015-12-10
      • 2019-08-05
      • 2020-02-09
      • 1970-01-01
      • 1970-01-01
      • 2020-05-02
      • 2021-12-29
      相关资源
      最近更新 更多