我正在尝试从 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)
我正在尝试抓取“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/