目錄
作者github,該項目接口地址詳情
windows端
流程:
- 獲得網上免費接口並進行調用
- 數據清洗
- 數據導出爲表格
- 使用tkinter進行界面使用
代碼:
import requests
import json
import time, datetime
import xlwt
from tkinter import *
from tkinter.messagebox import *
import threading
import os
def cxk():
url = "https://lab.isaaclin.cn/nCoV/api/area?latest=1"
response = requests.get(url)
json_reads = json.loads(response.text)
ALL=json_reads["results"]
print(ALL)
newTime = datetime.datetime.fromtimestamp(ALL[0]["updateTime"]/1000.0).strftime("%Y-%m-%d-%H")
# print(newTime+"統計完成")
#創建工作博
wb =xlwt.Workbook(encoding='utf-8')
#括號內參數爲表名 ws 爲各省份表
ws = wb.add_sheet('%s時新型肺炎統計表'%newTime)
#括號內參數爲表名 wk 爲各市區表
wk = wb.add_sheet('%s時新型肺炎各省份統計表'%newTime)
# provinceName=[]#省份
# cityName=[]#下屬城市
# confirmedCount=[]#確診人數
# suspectedCount=[]#疑似病例
# curedCount=[]#治癒人數
# deadCount=[]#死亡人數
ws.write(0,0,label = '國家')
ws.write(0,1,label = '省份')
ws.write(0,2,label = '死亡人數')
ws.write(0,3,label = '治癒人數')
ws.write(0,4,label = '確診人數')
ws.write(0,5,label = '疑似人數')
wk.write(0,0,label = '省份')
wk.write(0,1,label = '下屬城市')
wk.write(0,2,label = '死亡人數')
wk.write(0,3,label = '治癒人數')
wk.write(0,4,label = '確診人數')
wk.write(0,5,label = '疑似人數')
k=1
l=1
for i in ALL:
# print(i["provinceName"]+": "+"死亡人數:"+str(i["deadCount"])+" 治癒人數:"+str(i["curedCount"])+" 確診人數:"+str(i["confirmedCount"])+" 疑似人數:"+str(i["suspectedCount"]))
ws.write(k,0,label = i["countryName"])
ws.write(k,1,label = i["provinceName"])
ws.write(k,2,label = i["deadCount"])
ws.write(k,3,label = i["curedCount"])
ws.write(k,4,label = i["confirmedCount"])
ws.write(k,5,label = i["suspectedCount"])
if i["countryName"]=="中國":
for j in i["cities"]:
wk.write(l,0,label = i["provinceName"])
wk.write(l,1,label = j["cityName"])
wk.write(l,2,label = j["deadCount"])
wk.write(l,3,label = j["curedCount"])
wk.write(l,4,label = j["confirmedCount"])
wk.write(l,5,label = j["suspectedCount"])
l+=1
# print(j["cityName"]+":\n 死亡人數:"+str(j["deadCount"])+" 治癒人數:"+str(j["curedCount"])+" 確診人數:"+str(j["confirmedCount"])+" 疑似人數:"+str(j["suspectedCount"]))
k+=1
# print('\n')
wb.save('新型肺炎%s時統計表.xls'%newTime)
showinfo(title='成功', message='今日數據統計完成!')
def fun():
th=threading.Thread(target=cxk)
th.setDaemon(True)#守護線程
th.start()
showinfo(title='提示', message='後臺數據統計已開始\n請稍等,完成後將會有提示!')
root = Tk()
root.title('加油!武漢!')
winWidth = 250
winHeight = 100
screenWidth = root.winfo_screenwidth()
screenHeight = root.winfo_screenheight()
x = int((screenWidth - winWidth) / 2)
y = int((screenHeight - winHeight) / 2)
# 設置窗口初始位置在屏幕居中
root.geometry("%sx%s+%s+%s" % (winWidth, winHeight, x, y))
Label(root,text="新型肺炎數據一鍵統計小程序").pack()
Button(text='統計', command=fun).pack()
Label(root,text="文件存儲位置").pack()
Label(root,text=os.getcwd()).pack()
root.mainloop()
截圖:
服務器端:
流程:
- 上傳源碼
- crontab進行定時控制
- 編寫配置文件
- 編寫腳本
代碼:
py源碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/2/5 8:40
# @Author : Cxk
# @File : cxk.py
import requests
import json
import time, datetime
import xlwt
def cxk():
url = "https://lab.isaaclin.cn/nCoV/api/area?latest=1"
response = requests.get(url)
json_reads = json.loads(response.text)
ALL=json_reads["results"]
# print(ALL)
newTime = datetime.datetime.fromtimestamp(ALL[0]["updateTime"]/1000.0).strftime("%Y-%m-%d-%H-%M")
# print(newTime+"統計完成")
#創建工作博
wb =xlwt.Workbook(encoding='utf-8')
#括號內參數爲表名 ws 爲各省份表
ws = wb.add_sheet('%s分新型肺炎統計表'%newTime)
#括號內參數爲表名 wk 爲各市區表
wk = wb.add_sheet('%s分新型肺炎各省份統計表'%newTime)
# provinceName=[]#省份
# cityName=[]#下屬城市
# confirmedCount=[]#確診人數
# suspectedCount=[]#疑似病例
# curedCount=[]#治癒人數
# deadCount=[]#死亡人數
ws.write(0,0,label = '國家')
ws.write(0,1,label = '省份')
ws.write(0,2,label = '死亡人數')
ws.write(0,3,label = '治癒人數')
ws.write(0,4,label = '確診人數')
ws.write(0,5,label = '疑似人數')
wk.write(0,0,label = '省份')
wk.write(0,1,label = '下屬城市')
wk.write(0,2,label = '死亡人數')
wk.write(0,3,label = '治癒人數')
wk.write(0,4,label = '確診人數')
wk.write(0,5,label = '疑似人數')
k=1
l=1
for i in ALL:
# print(i["provinceName"]+": "+"死亡人數:"+str(i["deadCount"])+" 治癒人數:"+str(i["curedCount"])+" 確診人數:"+str(i["confirmedCount"])+" 疑似人數:"+str(i["suspectedCount"]))
ws.write(k,0,label = i["country"])
ws.write(k,1,label = i["provinceName"])
ws.write(k,2,label = i["deadCount"])
ws.write(k,3,label = i["curedCount"])
ws.write(k,4,label = i["confirmedCount"])
ws.write(k,5,label = i["suspectedCount"])
if i["country"]=="中國":
for j in i["cities"]:
wk.write(l,0,label = i["provinceName"])
wk.write(l,1,label = j["cityName"])
wk.write(l,2,label = j["deadCount"])
wk.write(l,3,label = j["curedCount"])
wk.write(l,4,label = j["confirmedCount"])
wk.write(l,5,label = j["suspectedCount"])
l+=1
# print(j["cityName"]+":\n 死亡人數:"+str(j["deadCount"])+" 治癒人數:"+str(j["curedCount"])+" 確診人數:"+str(j["confirmedCount"])+" 疑似人數:"+str(j["suspectedCount"]))
k+=1
# print('\n')
wb.save('%s分新型肺炎統計表.xls'%newTime)
cxk()
腳本cron.sh
cd cxk_python
/root/cxk_python/flaskenv/bin/python3 /root/cxk_python/cxk.py > /root/cxk_python/make.out 2>&1 &
crontab -e配置文件
0 */12 * * * /bin/sh /root/cxk_python/cron.sh ###每隔12小時運行一次腳本