我正在做一个项目来获取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)
输出为“无”。我知道价格是在上面的函数中编写的,但我不知道如何获得价格。可以用Beautulsoup或else模块解决吗?在
你会看到这个页面的源代码
我们想要的
^{pr2}$span
在索引2中,用演示:
<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>
;