-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
26 lines (23 loc) · 1 KB
/
Copy pathapp.py
File metadata and controls
26 lines (23 loc) · 1 KB
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
import requests
import json
url = 'https://guilhermeonrails.github.io/api-restaurantes/restaurantes.json'
response = requests.get(url)
print(response)
if response.status_code == 200:
dados_json = response.json()
dados_do_restaurante = {} # dicionário
for item in dados_json:
nome_do_restaurante = item['Company']
if nome_do_restaurante not in dados_do_restaurante:
dados_do_restaurante[nome_do_restaurante] = [] # cria uma lista pra cada restaurante dentro do dicionário
dados_do_restaurante[nome_do_restaurante].append({
"item": item['Item'],
"price": item['price'],
"description": item['description'],
})
else:
print(f'Ocorreu um erro: {response.status_code}')
for nome_do_restaurante, dados in dados_do_restaurante.items():
nome_do_arquivo = f'{nome_do_restaurante}.json'
with open(nome_do_arquivo, 'w') as arquivo_restaurante:
json.dump(dados, arquivo_restaurante, indent=4)