gpt4 book ai didi

python - 读取 yahoofinancials 的输出

转载 作者:行者123 更新时间:2023-12-04 10:54:48 25 4
gpt4 key购买 nike

如何读取 yahoofinancials 1.5 引用的输出。 [ 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}}

在这里,我想处理几个单一的数据项,但对我来说没有任何作用。请帮忙。

当我尝试解决“ebit”时:
    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'

我也想获得 EBITDA,从哪里获得?它包含在雅虎中。

我可以通过以下方式获取一些单个数据项:get_total_revenue() 和 get_net_income(),但仅限于去年。是否可以将 year 和 or quorter 作为参数。

我使用的是 Windows 10 和 Python 3.7

任何帮助都感激不尽。

最佳答案

当您在@PaulLo 中删除答案时-您无法直接获得 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

关于python - 读取 yahoofinancials 的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59278589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com