私は以下のコードを使用してYahoo Financeからデータを取得しようとしていますが、エラーが表示されます。Pythonを使用して損益計算書データを引き出す
私は端からデバッグしようとしましたが、それは
タイトル= soup.find(テキスト=パターン、「強い」)空白
を#returning findコマンドを使用するときにタイトルが空白として来ているに見えますコード:
# -*- coding: utf-8 -*-
"""
"""
#Read income statement to calculate ratios
from bs4 import BeautifulSoup
import requests
import re,sys
myurl = "https://finance.yahoo.com/q/is?s=AAPL&annual"
html = requests.get(myurl).content
soup = BeautifulSoup(html)
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
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.")
print(periodic_figure_values(financials_soup("AAPL", "is"), "Income Tax Expense"))
"""throws error: An exception has occurred, use %tb to see the full traceback.
SystemExit: Invalid figure 'Income Tax Expense' passed."""
代わりにQuandlの使用を検討しましたか? (FWIW、私はこれらの企業と提携していない) –