读取来自 yahoofinancials 的输出

2024-10-02 22:35:16 发布

您现在位置:Python中文网/ 问答频道 /正文

如何读取yahoofinancials 1.5 ref.[https://pypi.org/project/yahoofinancials/]
的输出 我是Python新手,需要帮助处理从以下位置返回的JSON数据输出:

from yahoofinancials import YahooFinancials  
yahoo_financials = YahooFinancials('AAPL')  
print(yahoo_financials.get_financial_stmts('annual', 'income'))**

非有序输出的前几行:

{'incomeStatementHistory': {'AAPL': [{'2019-09-28': {'researchDevelopment': 16217000000, 'effectOfAccountingCharges': None, 'incomeBeforeTax': 65737000000, 'minorityInterest': None, 'netIncome': 55256000000, 'sellingGeneralAdministrative': 18245000000, 'grossProfit': 98392000000, 'ebit': 63930000000, 'operatingIncome': 63930000000, 'otherOperatingExpenses': None, 'incomeBeforeTax': 65737000000, 'minorityInterest': None, 'netIncome': 55256000000, . . .  . .}  

我的输出是单引号,而不是双引号预期,但包含所有4年,好的。你知道吗

我可以通过以下方式解决一年的问题:

data = yahoofinancials.getfinancial_stmts('annual', 'income')   
data['incomeStatementHistory']['DSV.CO'][0]  

获取每行中的有序输出键值:

{'2018-12-31': {'researchDevelopment': None'effectOfAccountingCharges': None,   
  'incomeBeforeTax': 5201000000,   
  'minorityInterest': -29000000,   
  'netIncome': 4000000000,   
  'sellingGeneralAdministrative': 11301000000,   
  'grossProfit': 17489000000,   
'ebit': 5426000000,   
'operatingIncome': 5426000000,   
'otherOperatingExpenses': None,   
'interestExpense': -355000000,   
'extraordinaryItems': None,   
'nonRecurring': None,   
'otherItems': None,   
'incomeTaxExpense': 1213000000,   
'totalRevenue': 79053000000,     
'totalOperatingExpenses': 73627000000,   
'costOfRevenue': 61564000000,   
'totalOtherIncomeExpenseNet': -225000000,   
'discontinuedOperations': None,   
'netIncomeFromContinuingOps': 3988000000,   
'netIncomeApplicableToCommonShares': 4000000000}}

在这里,我想处理几个单一的数据项,但没有什么适合我。请帮忙。你知道吗

当我试图解决“息税前利润”时:

    data['incomeStatementHistory']['DSV.CO'][0]['ebit']

我得到以下错误:

---------------------------------------------------------------------------
KeyError                          Traceback (most recent call last)   
ipython-input-12-66fa46d51257> in module>   

-----> 1 data['incomeStatementHistory']['DSV.CO'][0]['ebit']

KeyError: 'ebit'

我也想得到息税折旧摊销前利润,我从哪里得到它?它是包括从雅虎。你知道吗

我可以通过以下方式获得一些数据项:get\u total\u revenue()和get\u net\u income(),但仅限于去年。可以将年份和或quorter作为参数。你知道吗

我使用的是windows10和python3.7

任何帮助都将不胜感激。你知道吗


Tags: nonedatagetyahooconbspincomeyahoofinancials
1条回答
网友
1楼 · 发布于 2024-10-02 22:35:16

当你进入@PaulLo deleted answer时-你不能直接得到ebit,因为有一个日期

 data['incomeStatementHistory']['AAPL'][0]['2019-09-28']['ebit']
 data['incomeStatementHistory']['AAPL'][1]['2019-09-29']['ebit']
 data['incomeStatementHistory']['AAPL'][2]['2019-09-30']['ebit']

但是可以用.keys()看到不同的日期

 print( data['incomeStatementHistory']['AAPL'][0].keys() ) # ['2019-09-28']
 print( data['incomeStatementHistory']['AAPL'][1].keys() ) # ['2019-09-29']
 print( data['incomeStatementHistory']['AAPL'][2].keys() ) # ['2019-09-30']

要解决此问题,可以使用.keys().items().values()中的日期

from yahoofinancials import YahooFinancials  

yahoo_financials = YahooFinancials('AAPL')
data = yahoo_financials.get_financial_stmts('annual', 'income') 

#print( data['incomeStatementHistory']['AAPL'][0].keys() ) # ['2019-09-28']
#print( data['incomeStatementHistory']['AAPL'][1].keys() ) # ['2019-09-29']
#print( data['incomeStatementHistory']['AAPL'][2].keys() ) # ['2019-09-30']

for item in data['incomeStatementHistory']['AAPL']:
    for key, val in item.items():
        print(key, val['ebit'])

结果:

2019-09-28 63930000000
2018-09-29 70898000000
2017-09-30 61344000000
2016-09-24 60024000000

相关问题 更多 >