Last active
July 5, 2022 20:50
-
-
Save kalashnikov/c1a789ab4a5252cc095290bce7f08e3e to your computer and use it in GitHub Desktop.
Process financials data from Gurufocus API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import pandas as pd | |
import requests as r | |
import os | |
import sys | |
# | |
# Load data using Gurufocus API | |
# Process data into DataFrame object and save as csv format | |
# | |
def get_financials_data_by_token(name): | |
data_json = r.get('https://www.gurufocus.com/api/public/user/{}/stock/{}/financials'.format(os.environ['GURU_TOKEN'], name), headers={'User-Agent': 'Mozilla/5.0'}).json() | |
for t in data_json['financials'].keys(): | |
df_list = [] | |
# Accmulate data into DataFrame list | |
keys = list(data_json['financials'][t].keys()) | |
keys.remove('Fiscal Year') # Used as Index | |
for k in keys: | |
df_list.append(pd.DataFrame(data=data_json['financials'][t][k], index=data_json['financials'][t]['Fiscal Year'])) | |
# Concate lists | |
df = pd.concat(df_list, axis=1) | |
df.to_csv('{}_{}.csv'.format(name, t)) | |
if __name__ == '__main__': | |
if len(sys.argv) == 1: | |
print("Please provide token.") | |
exit() | |
token = sys.argv[1] | |
get_financials_data_by_token(token) | |
print('### Finish load {} financials data and output to csv files.'.format(token)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment