【发布时间】:2016-05-28 02:50:43
【问题描述】:
我正在尝试使用 Python 从Yahoo Finance 的损益表中抓取数据。具体来说,假设我想要most recent figure of Net Income of Apple。
数据由一堆嵌套的 HTML 表格构成。我正在使用 requests 模块来访问它并检索 HTML。
我正在使用BeautifulSoup 4 来筛选 HTML 结构,但我不知道如何得到这个数字。
Here 是使用 Firefox 分析的截图。
到目前为止我的代码:
from bs4 import BeautifulSoup
import requests
myurl = "https://finance.yahoo.com/q/is?s=AAPL&annual"
html = requests.get(myurl).content
soup = BeautifulSoup(html)
我尝试过使用
all_strong = soup.find_all("strong")
然后获取第 17 个元素,恰好是包含我想要的图形的元素,但这似乎远非优雅。像这样的:
all_strong[16].parent.next_sibling
...
当然,目标是使用BeautifulSoup 搜索我需要的数字的名称(在本例中为“净收入”),然后获取数字本身 在 HTML 表的同一行中。
我非常感谢有关如何解决此问题的任何想法,请记住,我想应用该解决方案从其他雅虎财经页面检索大量其他数据。
解决方案/扩展:
@wilbur 下面的解决方案奏效了,我对其进行了扩展,以便能够在 any 的财务页面(即 @987654327 @、Balance Sheet 和 Cash Flow Statement) 适用于任何上市公司。 我的功能如下:
def periodic_figure_values(soup, yahoo_figure):
values = []
pattern = re.compile(yahoo_figure)
title = soup.find("strong", text=pattern) # works for the figures printed in bold
if title:
row = title.parent.parent
else:
title = soup.find("td", text=pattern) # works for any other available figure
if title:
row = title.parent
else:
sys.exit("Invalid figure '" + yahoo_figure + "' passed.")
cells = row.find_all("td")[1:] # exclude the <td> with figure name
for cell in cells:
if cell.text.strip() != yahoo_figure: # needed because some figures are indented
str_value = cell.text.strip().replace(",", "").replace("(", "-").replace(")", "")
if str_value == "-":
str_value = 0
value = int(str_value) * 1000
values.append(value)
return values
yahoo_figure 变量是一个字符串。显然,这必须与雅虎财经上使用的人物名称完全相同。
要传递soup 变量,我首先使用以下函数:
def financials_soup(ticker_symbol, statement="is", quarterly=False):
if statement == "is" or statement == "bs" or statement == "cf":
url = "https://finance.yahoo.com/q/" + statement + "?s=" + ticker_symbol
if not quarterly:
url += "&annual"
return BeautifulSoup(requests.get(url).text, "html.parser")
return sys.exit("Invalid financial statement code '" + statement + "' passed.")
使用示例 -- 我想从最后可用的损益表中获取 Apple Inc. 的所得税费用:
print(periodic_figure_values(financials_soup("AAPL", "is"), "Income Tax Expense"))
输出:[19121000000, 13973000000, 13118000000]
您还可以从soup 获取期间结束的日期,并创建一个字典,其中日期是键,数字是值,但这会使这篇文章太长。
到目前为止,这似乎对我有用,但我总是感谢建设性的批评。
【问题讨论】:
标签: python html beautifulsoup yahoo-finance