我正在一个项目上以获取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)
输出为“无”。我知道价格写在上面的函数中,但我不知道如何获得价格。可以通过beautifulsoup还是其他模块来解决?
答案 0 :(得分:0)
查看页面源代码,您将看到html
<div class="table detail">
.....
<div class="tl">即市走勢 <span class="description">前收市價</span>
.....
<td>買入價(延遲*)<span>82.15</span></td>
我们想要的span
在索引2中,请选择
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>