I’m trying to write down a python script that allow me to get some items of financial statement from Yahoo.I’ve tried with yahoofinancials library, but I can get only an entire page of data: For istance,with this code:
JavaScript
x
4
1
from yahoofinancials import YahooFinancials
2
yahoo_financials = YahooFinancials('AAPL')
3
print(yahoo_financials.get_financial_stmts('annual', 'balance'))
4
I will get this:
JavaScript
1
36
36
1
{
2
"balanceSheetHistory": {
3
"AAPL": [
4
{
5
"2016-09-24": {
6
"otherCurrentLiab": 8080000000,
7
"otherCurrentAssets": 8283000000,
8
"goodWill": 5414000000,
9
"shortTermInvestments": 46671000000,
10
"longTermInvestments": 170430000000,
11
"cash": 20484000000,
12
"netTangibleAssets": 119629000000,
13
"totalAssets": 321686000000,
14
"otherLiab": 36074000000,
15
"totalStockholderEquity": 128249000000,
16
"inventory": 2132000000,
17
"retainedEarnings": 96364000000,
18
"intangibleAssets": 3206000000,
19
"totalCurrentAssets": 106869000000,
20
"otherStockholderEquity": 634000000,
21
"shortLongTermDebt": 11605000000,
22
"propertyPlantEquipment": 27010000000,
23
"deferredLongTermLiab": 2930000000,
24
"netReceivables": 29299000000,
25
"otherAssets": 8757000000,
26
"longTermDebt": 75427000000,
27
"totalLiab": 193437000000,
28
"commonStock": 31251000000,
29
"accountsPayable": 59321000000,
30
"totalCurrentLiabilities": 79006000000
31
}
32
}
33
]
34
}
35
}
36
I want to get every single element, such as “cash” and put it in a variable or an array with all these data,in order to get the single number. So,for example, if I would get “cash”,I would have a variable or an array/list that allow me to get the number(in this case 20484000000,for cash). I hope I’ve made myself clear. Someone knows how to do it?Thank you.
Advertisement
Answer
Since the output is in json format we must work with json.
JavaScript
1
6
1
from yahoofinancials import YahooFinancials
2
import json
3
yahoo_financials = YahooFinancials('AAPL')
4
w = yahoo_financials.get_financial_stmts('annual', 'balance')
5
print(w["balanceSheetHistory"]["AAPL"][2]['2019-09-28']['totalLiab'])
6
change ‘totalLiab’ to get desired data and to change ‘2019-09-28’ you must also change [2].