2017-05-22 1 views
0

나는 자습서를 따라하고 requests.get()의 출력구문 분석 requests.get() 출력

내 목표 아래 역사 암호 - 당기하는 API에 연결하는 것입니다 구문 분석에 붙어 추가 분석을 위해 판다 데이터 프레임에 넣습니다.

[API : https://www.cryptocompare.com/api/#-api-data-histoday-]

여기 내가 가진거야.

import requests 
response = requests.get("https://min-api.cryptocompare.com/data/histodayfsym=ETC&tsym=USD&limit=10&aggregate=3&e=CCCAGG") 



print(response.text) 

지금은 dataframe로 출력하고자 ...

pd.DataFrame.from_dict(response) 

하지만 좀 .. PandasError : DataFrame 생성자 제대로 호출되지 않습니다! jonrsharpe이 언급 그러나으로

import requests 
from json import loads 

response = requests.get("https://min-api.cryptocompare.com/data/histodayfsym=ETC&tsym=USD&limit=10&aggregate=3&e=CCCAGG") 

dic = loads(response.text) 

print(type(dic)) 

pd.DataFrame.from_dict(dic) 

이 훨씬 더 간단한 방법은 다음과 같습니다 :

+0

'response'은 사전되지 않습니다 - 아마 .from_dict (response.json())'? – jonrsharpe

+0

그건 좀 더 가까이에있어! 이제는 DF에 있지만 키와 값은 모두 순서가 맞지 않습니다 ... 타임 스탬프가 있어야 datetime 형식이됩니다. 그러면 인덱스를 호출합니다. – user2179795

답변

1

당신은 DICT로 변환하는 JSON 패키지를 사용할 수 있습니다

import requests 

response = requests.get("https://min-api.cryptocompare.com/data/histodayfsym=ETC&tsym=USD&limit=10&aggregate=3&e=CCCAGG") 


print(type(response.json())) 
pd.DataFrame.from_dict(response.json()) 
+0

실제로 : http : //docs.python -requests.org/en/master/user/quickstart/#json-response-content – jonrsharpe

+0

실제로, 나는 고침을 받았다. :) 이것을 반영하기 위해 업데이트 대답이있다. – RandomHash