beautifulSoup 的不一致行为

inconsistent behavior of beautifulSoup

我正在尝试制作一个 python 文件来提供实时库存更新。 我尝试 for l in code.findAll('span', {'id' : 'ChangePercent'}): 为 :

<span id="ChangePercent">-0.71%</span>

它起作用了, 来源:money.rediff.com/ 但在雅虎财经上使用相同的技术不起作用.. 即

price = code.findAll('span', {'class' : 'yfi-price-change-green'})

找不到:<span class="yfi-price-change-green">(1.95%)</span>

来源:finance.yahoo.com/

两个代码: 重新调整:http://ideone.com/kslILJ 雅虎:http://ideone.com/egGQLv

您在 Yahoo Finance 页面上查找的值会通过浏览器中的流 API 调用定期更新。您不会仅通过使用 urllibrequests.

请求 Reliance Industries Ltd (RELIANCE.NS) url 来获得这些值

最不复杂的选项是使用 selenium:

自动化一个真正的浏览器
>>> from selenium import webdriver
>>>
>>> driver = webdriver.Chrome()
>>> driver.get('https://in.finance.yahoo.com/q?s=RELIANCE.NS&ql=0')
>>> for element in driver.find_elements_by_class_name('yfi-price-change-green'):
...     print(element.text)
... 
0.55%
0.40%