7/2/2017 - 4:52 AM

Process financials data from Gurufocus API

Process financials data from Gurufocus API

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.")

    token = sys.argv[1]
    print('### Finish load {} financials data and output to csv files.'.format(token))