如何使用BeautifulSoup获取网站实时股价?

How to use BeautifulSoup to get real-time stock price on website?

我正在做一个项目来获取 http://www.jpmhkwarrants.com/en_hk/market-statistics/underlying/underlying-terms/code/1 上的实时股票价格。我在网上搜索并尝试了几种获取价格的方法,但仍然失败。这是我的代码:

def getStockPrice():
      url = "http://www.jpmhkwarrants.com/zh_hk/market-statistics/underlying/underlying-terms/code/1" 
       r = urlopen(url)
      soup = BeautifulSoup(r.read(), 'lxmll)
      price = soup.find(id = "real_time_box").find({"span", "class":"price"})
      print(price)

输出为"None"。我知道价格是在上面的函数中编写的,但我不知道如何获得价格。可以通过beautifulsoup或者模块解决吗?

查看页面源你会看到html这样

<div class="table detail">
    .....
    <div class="tl">即市走勢 <span class="description">前收市價</span>
    .....
    <td>買入價(延遲*)<span>82.15</span></td>

我们想要的span在索引2中,select它和

price = soup.select('.table.detail td span')[1]
print(price.text)

演示:

<script type="text/javascript" src="//cdn.datacamp.com/dcl-react.js.gz"></script>

<div data-datacamp-exercise data-lang="python">
 <code data-type="sample-code">
  from bs4 import BeautifulSoup
  from urllib.request import urlopen

  def getStockPrice():
        url = "http://www.jpmhkwarrants.com/zh_hk/market-statistics/underlying/underlying-terms/code/1" 
        r = urlopen(url)
        soup = BeautifulSoup(r.read(), 'html.parser')
        price = soup.select('.table.detail td span')[1]
        print(price.text)

  getStockPrice()
 </code>
</div>