-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample_api.py
42 lines (31 loc) · 945 Bytes
/
example_api.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
35
36
37
38
39
40
41
42
import requests
import json
import csv
URL = "http://makeup-api.herokuapp.com/api/v1/products.json"
def get_data(url):
'''Get the data by API'''
response = requests.get(url).json()
data = json.dumps(response, indent=4, sort_keys=True)
return data
def save_json(data):
'''Save to a json file'''
file = open('makeup_data.json', encoding='utf-8', mode='w')
json.dump(data, file)
file.close()
def save_csv(data):
'''Save to a csv file'''
products = json.loads(data)
products_data = open('products.csv', 'w', newline='', encoding='utf-8')
csvwriter = csv.writer(products_data)
count = 0
for pro in products:
if count == 0:
header = pro.keys()
csvwriter.writerow(header)
count += 1
csvwriter.writerow(pro.values())
products_data.close()
if __name__ == "__main__":
data = get_data(URL)
save_json(data)
save_csv(data)