-
Notifications
You must be signed in to change notification settings - Fork 8
/
json_collect.py
37 lines (25 loc) · 1.88 KB
/
json_collect.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
import urllib.request, json
import pandas as pd
from pandas.io.json import json_normalize
# coleta dados api
with urllib.request.urlopen("https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/odata/ExpectativaMercadoMensais?$top=1000&$orderby=Data%20desc&$format=json&$select=Indicador,Data,DataReferencia,Media,Mediana,Minimo,Maximo,numeroRespondentes") as url:
monthly_data = json.loads(url.read().decode())
with urllib.request.urlopen("https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/odata/ExpectativasMercadoTrimestrais?$top=100&$orderby=Data%20desc&$format=json&$select=Indicador,Data,DataReferencia,Media,Mediana,Minimo,Maximo,numeroRespondentes") as url:
quarterly_data = json.loads(url.read().decode())
with urllib.request.urlopen("https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/odata/ExpectativasMercadoInflacao12Meses?$top=100&$orderby=Data%20desc&$format=json&$select=Indicador,Data,Suavizada,Media,Mediana,Minimo,Maximo,numeroRespondentes") as url:
twelve_months_data = json.loads(url.read().decode())
with urllib.request.urlopen("https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/odata/ExpectativasMercadoAnuais?$top=1000&$orderby=Data%20desc&$format=json&$select=Indicador,IndicadorDetalhe,Data,DataReferencia,Media,Mediana,Minimo,Maximo,numeroRespondentes") as url:
annual_data = json.loads(url.read().decode())
# transforma json para df
monthly_data_df = pd.DataFrame.from_dict(
monthly_data['value'], orient='columns')
print(monthly_data_df.head)
quarterly_data_df = pd.DataFrame.from_dict(
quarterly_data['value'], orient='columns')
print(quarterly_data_df.head)
twelve_months_data_df = pd.DataFrame.from_dict(
twelve_months_data['value'], orient='columns')
print(twelve_months_data_df.head)
annual_data_df = pd.DataFrame.from_dict(
annual_data['value'], orient='columns')
print(annual_data_df.head)