-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreading_from_api.py
47 lines (38 loc) · 1.08 KB
/
reading_from_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
'''
Financial Modeling Prep is a new concept that informs you about stock markets information (news, currencies and stock prices).
This organization runs a free api that we will read data in from
'''
from urllib.request import urlopen
import json
from pandas import DataFrame
def get_jsonparsed_data(ticker):
"""
Receive the content of ``url``, parse it as JSON and return the object.
Parameters
----------
url : str
Returns
-------
dict
"""
url = get_url(ticker)
response = urlopen(url)
data = response.read().decode("utf-8")
return json.loads(data)
def get_url(ticker):
url = 'https://financialmodelingprep.com/api/financials/income-statement/' + ticker + '?datatype=json'
return url
def get_company_dataframe(ticker):
'''
this is just a bonus :)
'''
data = get_jsonparsed_data(ticker)
df = DataFrame(data[ticker])
return df
ticker = 'FB'
print("JSON:")
data = get_jsonparsed_data(ticker)
print(data)
print('\nthat\'s hard to read... let\'s try a dataframe:')
data = get_company_dataframe(ticker)
print(data.head())