我正在尝试制作一个提供实时股票更新的 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 调用定期更新。您不会通过使用 urllib
或 requests
请求 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%
关于python - beautifulSoup 的不一致行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28081632/