我正试图写一个python脚本,让我可以从雅虎获得一些财务报表项目。我尝试过yahoofinancials库,但我只能获得一整页的数据:对于istance,使用此代码:
from yahoofinancials import YahooFinancials
yahoo_financials = YahooFinancials('AAPL')
print(yahoo_financials.get_financial_stmts('annual', 'balance'))
我会得到这个:
{
"balanceSheetHistory": {
"AAPL": [
{
"2016-09-24": {
"otherCurrentLiab": 8080000000,
"otherCurrentAssets": 8283000000,
"goodWill": 5414000000,
"shortTermInvestments": 46671000000,
"longTermInvestments": 170430000000,
"cash": 20484000000,
"netTangibleAssets": 119629000000,
"totalAssets": 321686000000,
"otherLiab": 36074000000,
"totalStockholderEquity": 128249000000,
"inventory": 2132000000,
"retainedEarnings": 96364000000,
"intangibleAssets": 3206000000,
"totalCurrentAssets": 106869000000,
"otherStockholderEquity": 634000000,
"shortLongTermDebt": 11605000000,
"propertyPlantEquipment": 27010000000,
"deferredLongTermLiab": 2930000000,
"netReceivables": 29299000000,
"otherAssets": 8757000000,
"longTermDebt": 75427000000,
"totalLiab": 193437000000,
"commonStock": 31251000000,
"accountsPayable": 59321000000,
"totalCurrentLiabilities": 79006000000
}
}
]
}
}
我想得到每一个元素,比如";现金";然后把它放在一个变量或一个包含所有这些数据的数组中,以获得单个数字。因此,例如,如果我得到";现金";,我会有一个变量或数组/列表,允许我获得数字(在本例中为20484000000,表示现金(。我希望我已经说清楚了。有人知道怎么做吗?非常感谢。
由于输出是json格式,我们必须使用json。
from yahoofinancials import YahooFinancials
import json
yahoo_financials = YahooFinancials('AAPL')
w = yahoo_financials.get_financial_stmts('annual', 'balance')
print(w["balanceSheetHistory"]["AAPL"][2]['2019-09-28']['totalLiab'])
更改"totalLiab"以获得所需数据,要更改"2019-09-28",您还必须更改[2]。