-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
35 lines (30 loc) · 1.07 KB
/
run.py
File metadata and controls
35 lines (30 loc) · 1.07 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
27
28
29
30
31
32
33
34
35
import requests
import json
import pandas as pd
testfile = open("test.json", "a", encoding='utf-8')
url = "https://covid19.ddc.moph.go.th/api/Cases/round-3-line-lists?page=" #url end with ?page=
print("=======================\nPagination JSON API\nOutput: .json, .csv\n=======================")
startat = int(input("Start page: "))
endat = int(input("End page(0 for all of API): "))
if(endat == 0):
response = requests.get(url + "1")
lastpageurl = response.json()['links']['last']
lastpagenum = lastpageurl.split("=")[1]
lastpagenum = int(lastpagenum)
endat = lastpagenum
if(endat > lastpagenum):
endat = lastpagenum
#for i in range(1, int(lastpagenum)+1):
for i in range(startat, endat+1):
re = requests.get(url + str(i))
rawget = json.dumps(re.json()['data'], ensure_ascii=False)
if(i == 1):
rawget = rawget[:-1] + ","
elif(i == endat):
rawget = rawget[1:]
else:
rawget = rawget[1:-1] + ","
testfile.write(rawget)
print("geting page " + str(i))
df = pd.read_json('test.json')
df.to_csv('test.csv', index = None)