-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrfsp.py
55 lines (41 loc) · 1.39 KB
/
rfsp.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
43
44
45
46
47
48
49
50
51
52
53
54
55
import requests
import os
import csv
import time
def get_spots_data():
url = 'https://shanghaicity.openservice.kankanews.com/public/tour/filterinfo2'
response = requests.get(url, headers={'user-agent': 'rfsp/0.0.1'})
spots = response.json()
return spots
def getCSVHeader():
return ['景点', '级别', '开放时间', '数据更新时间', '当前客流', '瞬时最大承载量']
def getCSVRow(fieldnames, spot):
dic = {
fieldnames[0]: spot['NAME'],
fieldnames[1]: spot['GRADE'],
fieldnames[2]: spot['START_TIME'] + '~' + spot['END_TIME'],
fieldnames[3]: spot['TIME'],
fieldnames[4]: spot['NUM'],
fieldnames[5]: spot['MAX_NUM']
}
return dic
def writeCSV(csv_name, spot_list):
exist = os.path.exists(csv_name)
with open(csv_name, 'a') as csvfile:
fieldnames = getCSVHeader()
csv_writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
if not exist:
csv_writer.writeheader()
for spot in spot_list:
dic = getCSVRow(fieldnames, spot)
csv_writer.writerow(dic)
def requestAndCSV():
spotList = get_spots_data()
writeCSV('spot.csv', spotList)
def main():
sleep_input = input("输入多少秒获取一次数据(s),如一分钟输入60:")
sleep_time = int(sleep_input)
while True:
requestAndCSV()
time.sleep(sleep_time)
main()