目录
2019-nCoV新型肺炎数据一键获取并布置在云服务器上每隔12小时获取一次
2019-nCoV新型肺炎数据一键获取并布置在云服务器上每隔12小时获取一次
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["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)
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小时运行一次脚本
来源:CSDN
作者:半盏清茶℡
链接:https://blog.csdn.net/Cxk___/article/details/104186543