我想从雅虎财经抓取财务报表数据来进行财务分析。但是,该代码根本不检索任何数据,而是跳过所有代码。你能帮我修一下吗?
代码如下(安装所有需要的包):
`## Competitor Analysis
import yahoo_fin.stock_info as yf
import pandas as pd
import matplotlib.pyplot as plt
profitability_score = 0
tickers = yf.tickers_sp500()
def get_data(ticker):
balance_sheet = yf.get_balance_sheet(ticker)
income_statement = yf.get_income_statement(ticker)
years = balance_sheet.columns
return balance_sheet, income_statement, years
# Analysis of profitability
# Define the function profitability
# Define the calculation for "Return on Equity" by calculating necessary intermediate results first
def profitability(income_statement, balance_sheet):
net_income = income_statement[years[0]]['netIncome']
net_income_py = income_statement[years[1]]['netIncome']
net_income_py2 = income_statement[years[2]]['netIncome']
net_sales = income_statement[years[0]]['totalRevenue']
net_sales_py = income_statement[years[1]]['totalRevenue']
net_sales_py2 = income_statement[years[2]]['totalRevenue']
beg_assets = balance_sheet[years[1]]['totalAssets']
beg_assets_py = balance_sheet[years[2]]['totalAssets']
beg_assets_py2 = balance_sheet[years[3]]['totalAssets']
end_assets = balance_sheet[years[0]]['totalAssets']
end_assets_py = balance_sheet[years[1]]['totalAssets']
end_assets_py2 = balance_sheet[years[2]]['totalAssets']
end_equity = balance_sheet[years[0]]['totalStockholderEquity']
end_equity_py = balance_sheet[years[1]]['totalStockholderEquity']
end_equity_py2 = balance_sheet[years[2]]['totalStockholderEquity']
profit_margin = net_income/net_sales
profit_margin_py = net_income_py/net_sales_py
profit_margin_py2 = net_income_py2/net_sales_py2
average_total_assets = (beg_assets + end_assets)/2
average_total_assets_py = (beg_assets_py + end_assets_py)/2
average_total_assets_py2 = (beg_assets_py2 + end_assets_py2)/2
asset_turnover = net_sales/average_total_assets
asset_turnover_py = net_sales_py/average_total_assets_py
asset_turnover_py2 = net_sales_py2/average_total_assets_py2
financial_leverage = end_assets/end_equity
financial_leverage_py = end_assets_py/end_equity_py
financial_leverage_py2 = end_assets_py2/end_equity_py2
return_on_equity = profit_margin * asset_turnover * financial_leverage
return_on_equity_py = profit_margin_py * asset_turnover_py * financial_leverage_py
return_on_equity_py2 = profit_margin_py2 * asset_turnover_py2 * financial_leverage_py2
return profit_margin, profit_margin_py, profit_margin_py2, return_on_equity, return_on_equity_py, return_on_equity_py2
# Analysis of Leverage
# repeat the procedure to define the financial leverage and the degree of operating leverage
def leverage(income_statement, balance_sheet):
end_assets = balance_sheet[years[0]]['totalAssets']
end_assets_py = balance_sheet[years[1]]['totalAssets']
end_assets_py2 = balance_sheet[years[2]]['totalAssets']
end_equity = balance_sheet[years[0]]['totalStockholderEquity']
end_equity_py = balance_sheet[years[1]]['totalStockholderEquity']
end_equity_py2 = balance_sheet[years[2]]['totalStockholderEquity']
operating_income=income_statement[years[0]]['operatingIncome']
operating_income_py=income_statement[years[1]]['operatingIncome']
operating_income_py2=income_statement[years[2]]['operatingIncome']
operating_income_py3=income_statement[years[3]]['operatingIncome']
net_income = income_statement[years[0]]['netIncome']
net_income_py = income_statement[years[1]]['netIncome']
net_income_py2 = income_statement[years[2]]['netIncome']
net_income_py3 = income_statement[years[3]]['netIncome']
financial_leverage = end_assets/end_equity
financial_leverage_py = end_assets_py/end_equity_py
financial_leverage_py2 = end_assets_py2/end_equity_py2
degree_of_operating_leverage = (operating_income-operating_income_py)/(net_income-net_income_py)
degree_of_operating_leverage_py = (operating_income_py-operating_income_py2)/(net_income_py-net_income_py2)
degree_of_operating_leverage_py2 = (operating_income_py2-operating_income_py3)/(net_income_py2-net_income_py3)
return financial_leverage, financial_leverage_py, financial_leverage_py2, degree_of_operating_leverage, degree_of_operating_leverage_py, degree_of_operating_leverage_py2
#Analysis of Liqudiity
# Analysis of Liqudiity
# define the current ratio and the quick ratio
def liquidity(balance_sheet):
current_assets = balance_sheet[years[0]]['totalCurrentAssets']
current_assets_py = balance_sheet[years[1]]['totalCurrentAssets']
current_assets_py2 = balance_sheet[years[2]]['totalCurrentAssets']
current_liabilities = balance_sheet[years[0]]['totalCurrentLiabilities']
current_liabilities_py = balance_sheet[years[1]]['totalCurrentLiabilities']
current_liabilities_py2 = balance_sheet[years[2]]['totalCurrentLiabilities']
cash_and_eq = balance_sheet[years[0]]['cash']
cash_and_eq_py = balance_sheet[years[1]]['cash']
cash_and_eq_py2 = balance_sheet[years[2]]['cash']
accounts_rec = balance_sheet[years[0]]['netReceivables']
accounts_rec_py = balance_sheet[years[1]]['netReceivables']
accounts_rec_py2 = balance_sheet[years[2]]['netReceivables']
current_ratio = current_assets / current_liabilities
current_ratio_py = current_assets_py / current_liabilities_py
current_ratio_py2 = current_assets_py2 / current_liabilities_py2
quick_ratio = (cash_and_eq+accounts_rec) / current_liabilities
quick_ratio_py = (cash_and_eq_py+accounts_rec_py) / current_liabilities_py
quick_ratio_py2 = (cash_and_eq_py2+accounts_rec_py2) / current_liabilities_py2
return current_ratio, current_ratio_py, current_ratio_py2, quick_ratio, quick_ratio_py, quick_ratio_py2
# Get Results
# the user can fill in the Tickersymbols below
# the data is downloaded and the previously defined ratios are calculated
# results are merged in a dictionary
information = dict()
for ticker in ['AAPL', 'MSFT', 'GOOGL', 'AMZN', 'FB']:
try:
balance_sheet, income_statement, years = get_data(ticker)
profit_margin, profit_margin_py, profit_margin_py2, return_on_equity, return_on_equity_py, return_on_equity_py2 = profitability(income_statement, balance_sheet)
financial_leverage, financial_leverage_py, financial_leverage_py2, degree_of_operating_leverage, degree_of_operating_leverage_py, degree_of_operating_leverage_py2 = leverage(income_statement, balance_sheet)
current_ratio, current_ratio_py, current_ratio_py2, quick_ratio, quick_ratio_py, quick_ratio_py2 = liquidity(balance_sheet)
information[ticker] = dict(RoE2021=return_on_equity,
RoE2020=return_on_equity_py,
RoE2019=return_on_equity_py2,
Fin_Leverage2021=financial_leverage,
Fin_Leverage2020=financial_leverage_py,
Fin_Leverage2019=financial_leverage_py2,
DoL2021=degree_of_operating_leverage,
DoL2020=degree_of_operating_leverage_py,
DoL2019=degree_of_operating_leverage_py2,
Current_Ratio2021=current_ratio,
Current_Ratio2020=current_ratio_py,
Current_Ratio2019=current_ratio_py2,
Quick_Ratio2021=quick_ratio,
Quick_Ratio2020=quick_ratio_py,
Quick_Ratio2019=quick_ratio_py2
)
except TypeError as e:
print('Ticker', ticker, 'not found, continue with next ticker')
# The try except function is used to respond to a specific error.
# Here the most likely error is an input error of the ticker.
# Therefore, only this error should be responded to with a specific message to inform the user
# all other errors should be displayed in the standard way
The answer is:
Ticker AAPL not found, continue with next ticker
Ticker MSFT not found, continue with next ticker
Ticker GOOGL not found, continue with next ticker
Ticker AMZN not found, continue with next ticker
Ticker FB not found, continue with next ticker
Thus, the following dataframe I want to create is empty
df = pd.DataFrame(information)
df.T
`
我试着用漂亮的汤、yfinance 等抓取数据来做到这一点——没有任何效果