python - beautifulSoup 的不一致行为

标签 python web-scraping beautifulsoup html-parsing findall

我正在尝试制作一个提供实时股票更新的 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%

关于python - beautifulSoup 的不一致行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28081632/

相关文章:

python - Google 趋势 - 配额限制 - IP 地址更改器

python - UnicodeEncodeError : 'ascii' codec can't encode character u'\u2026'

python - 过滤字典的字典

python - 使用 Chrome 时的 Selenium "selenium.common.exceptions.NoSuchElementException"

python - Pandas:在 Dataframe 中填充新列,其中其他 2 列匹配

python - 在绘图旁边显示(离散)颜色条作为(自动选择的)线条颜色的图例

Python/BS - 从存储在目录中的 html 文件获取 URL,保存到 CSV

Python 网页抓取 Zacks 网站错误 : [WinError 10054] An existing connection was forcibly closed by the remote host

python - 使用带循环的漂亮汤在 Python 中制作 Webscrape 交互式图表

python - 使用 BeautifulSoup 时出现 AttributeError : ResultSet object has no attribute 'prettify' .