Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 它使用Json在API上运行,_Python_Json_Pandas - Fatal编程技术网

Python 它使用Json在API上运行,

Python 它使用Json在API上运行,,python,json,pandas,Python,Json,Pandas,导入请求 导入json 将numpy作为np导入 作为pd进口熊猫 url=“” JC=requests.get(url).json() content=json.dumps(JC,indent=4,sort\u keys=True) 日期=[“宠物”,“应用”] 日期列表=[] 对于日期中的日期: JC=requests.get(“+dates).json() 日期_list.append(JC['growth']) dataset=pd.DataFrame(日期列表) 数据集.示例(5) 我

导入请求 导入json 将numpy作为np导入 作为pd进口熊猫

url=“” JC=requests.get(url).json()

content=json.dumps(JC,indent=4,sort\u keys=True)

日期=[“宠物”,“应用”]

日期列表=[]

对于日期中的日期: JC=requests.get(“+dates).json()

日期_list.append(JC['growth'])

dataset=pd.DataFrame(日期列表) 数据集.示例(5)

我得到了这个错误: KeyError回溯(最近一次呼叫最后一次) 在里面 6 JC=requests.get(“+dates).json() 7. ---->8日期列表。附加(JC['growth']) 9 10数据集=局部数据帧(日期列表)

关键错误:“增长”

import requests 
import json 
import numpy as np 
import pandas as pd

url = "https://financialmodelingprep.com/api/v3/financial-statement-growth/PETS" 
JC = requests.get(url).json()

content = json.dumps(JC, indent = 4, sort_keys=True)

dates = ["PETS", "AAPL", "APPL"]

dates_list =[]

for dates in dates: 

    JC = requests.get("https://financialmodelingprep.com/api/v3/financial-statement-growth/"+ dates).json()
    if JC == {}:
        print "invalid ticker ... "
    else:
        dates_list.append(JC['growth'])


dataset = pd.DataFrame(dates_list)
print dataset

首先,证券交易委员会数据库中不存在股票代码应用程序 然后api返回一个空目录,其中密钥增长不显示在其中。您应该捕获错误,您可以在将值添加到日期列表之前检查字典是否为空

控制台输出

fmp_team:fmp $ python stackoverflow.py 
invalid ticker ... 
                                                  0                                                  1   ...                                                 9                                                  10
0  {u'Weighted Average Shares Diluted Growth': u'...  {u'Weighted Average Shares Diluted Growth': u'...  ...  {u'Weighted Average Shares Diluted Growth': u'...                                               None
1  {u'Weighted Average Shares Diluted Growth': u'...  {u'Weighted Average Shares Diluted Growth': u'...  ...  {u'Weighted Average Shares Diluted Growth': u'...  {u'Weighted Average Shares Diluted Growth': u'...

[2 rows x 11 columns]