python - 使用 BeautifulSoup4 和 Python 3 解析 html 表

标签 python html parsing beautifulsoup

我正在尝试从 Yahoo Finance 抓取某些财务数据。具体在这种情况下,单个收入数字(类型:double)

这是我的代码:

from urllib.request import urlopen
from bs4 import BeautifulSoup
  
searchurl = "http://finance.yahoo.com/q/ks?s=AAPL"
f = urlopen(searchurl)
html = f.read()
soup = BeautifulSoup(html, "html.parser")

revenue = soup.find("div", {"class": "yfnc_tabledata1", "id":"yui_3_9_1_8_1456172462911_38"})
print (revenue)

来自 Chrome 的查看源代码检查如下所示: enter image description here

我正在尝试抓取“234.99B”数字,去掉“B”并将其转换为小数。我的“soup.find”行有问题,我哪里出错了?

最佳答案

使用 Revenue (ttm): 文本找到 td 元素并获取 next td sibling :

revenue = soup.find("td", text="Revenue (ttm):").find_next_sibling("td").text
print(revenue)

打印 234.99B

关于python - 使用 BeautifulSoup4 和 Python 3 解析 html 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35563586/

相关文章:

将函数应用于链表的 Pythonic 方式

javascript - 根据所选的 HTML 按钮更改 JS 变量的值?

javascript - 用 Java 解析 JavaScript

json - Haskell-Aeson : Getting "Nothing" when trying to decode JSON URL Req

python - 使用 Lucene (PyLucene) 查找单个字段项

python - 字典的字典到 DataFrame

python - 在字符上拆分,但保留该字符 - python

javascript - Fabricjs Canvas 有时会错误地加载对象?

PHP实时动态计算mysql结果之和

PHP DOMDocument - 获取 BODY 的 html 源代码