【云函数】全国疫情数据实时统计(包含部署教程)
本帖最后由 culprit 于 2021-2-2 21:24 编辑往期云函数
【修改版】腾讯云函数实现每日天气推送:https://www.52pojie.cn/thread-1270946-1-1.html
【云函数】每日推送舔狗日记:https://www.52pojie.cn/thread-1271888-1-1.html
【云函数】什么值得买超值优惠推送 :https://www.52pojie.cn/thread-1359554-1-1.html
云函数部署教程:
1.绑定自己的推送机器人,使用GitHub账号登陆即可
(1)server酱(支持微信推送):https://sc.ftqq.com/3.version
(2)coolpush(支持QQ推送、微信推送):https://cp.xuthus.cc/
(3)Qmsg酱 -- 推荐用这个,比较稳定(支持QQ推送):https://qmsg.zendee.cn/
2.登录腾讯云函数,选择新建
3.选择自定义创建,输入函数名称,运行环境选择Python3
4.选择在线编辑,输入入口函数名(重点),粘贴函数代码(图中马赛克机器人的key需自行修改,选择其一即可,不然无法使用)
5.进入函数代码,测试是否可以正常运行
6.创建触发器(云函数定时执行),何时触发可以自己修改,图中为例子
7.执行结果
函数代码:
import requests,random,json
url = "https://c.m.163.com/ug/api/wuhan/app/data/list-total"
server_key = 'xxxxx'
coolpush_key = 'xxxxx'
qmsg_key = 'xxxxxx'
def UserAgent(): #随机获取请求头
user_agent_list = ['Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1464.0 Safari/537.36',
'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36',
'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.3319.102 Safari/537.36',
'Mozilla/5.0 (X11; CrOS i686 3912.101.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36',
'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36',
'Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36',
'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:17.0) Gecko/20100101 Firefox/17.0.6',
'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1468.0 Safari/537.36',
'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2224.3 Safari/537.36',
'Mozilla/5.0 (X11; CrOS i686 3912.101.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36']
UserAgent={'User-Agent': random.choice(user_agent_list)}
return UserAgent
def Get_Url(arg1,arg2):
url_json = requests.get(url=url,headers=UserAgent()).json()
today_confirm = str(url_json['data']['chinaTotal']['today']['confirm'])#全国累计确诊较昨日新增
today_input =str(url_json['data']['chinaTotal']['today']['input'])#全国较昨日新增境外输入
today_storeConfirm = str(url_json['data']['chinaTotal']['today']['storeConfirm'])#全国现有确诊较昨日
today_dead =str(url_json['data']['chinaTotal']['today']['dead'])#累计死亡较昨日新增
today_heal = str(url_json['data']['chinaTotal']['today']['heal'])#累计治愈较昨日新增
today_incrNoSymptom = str(url_json['data']['chinaTotal']['extData']['incrNoSymptom'])#无症状感染者较昨日
total_confirm = str(url_json['data']['chinaTotal']['total']['confirm'])# 全国累计确诊
total_input = str(url_json['data']['chinaTotal']['total']['input'])# 境外输入
total_dead = str(url_json['data']['chinaTotal']['total']['dead'])# 累计死亡
total_heal = str(url_json['data']['chinaTotal']['total']['heal'])# 累计治愈
total_storeConfirm = str(url_json['data']['chinaTotal']['total']['confirm'] - url_json['data']['chinaTotal']['total']['dead'] - url_json['data']['chinaTotal']['total']['heal'])# 全国现有确诊
total_noSymptom = str(url_json['data']['chinaTotal']['extData']['noSymptom'])#无症状感染者
lastUpdateTime = url_json['data']['lastUpdateTime']#截止时间
data ='-' * 8 +'全国疫情数据实时统计' + '-' * 8 + '\n统计截至时间:'+ lastUpdateTime +'\n' + '-' * 32 + '\n' + \
'累计确诊:' + total_confirm + ' ; ' + '较昨日:' + today_confirm + \
'\n现有确诊:' + total_storeConfirm + ' ; ' + '较昨日:' + today_storeConfirm + \
'\n累计死亡:' + total_dead + ' ; ' + '较昨日:' + today_dead + \
'\n累计治愈:' + total_heal + ' ; ' + '较昨日:' + today_heal + \
'\n境外输入:' + total_input + ' ; ' + '较昨日:' + today_input + \
'\n无症状感染者:' + total_noSymptom + ' ; ' + '较昨日:' + today_incrNoSymptom
print(data)
select_robots(0,data) #0为Qmsg推送,1为酷推推送,2为server酱推送。默认为0
def select_robots(i,data):
if i == 0:
HtmlPuch_Qmsg(data)
elif i == 1:
HtmlPuch_coolpush(data)
elif i == 2:
HtmlPuch_server(data)
else:
print('选择错误!')
def HtmlPuch_server(data): #server酱推送
url_key = "https://sc.ftqq.com/" + server_key + ".send"
push_data = {'text':"全国疫情数据实时统计",'desp':data}
html = requests.post(url_key,headers=UserAgent(),data=push_data)
def HtmlPuch_coolpush(data):#酷推推送
url_key = "https://push.xuthus.cc/send/" + coolpush_key
push_data = {'c':data}
html = requests.get(url=url_key,params=push_data,headers=UserAgent())
def HtmlPuch_Qmsg(data):#Qmsg推送
url_key = "https://qmsg.zendee.cn/send/" + qmsg_key
push_data = {'msg':data}
html = requests.get(url=url_key,params=push_data,headers=UserAgent())
@hack78
culprit 发表于 2021-2-2 21:00
我测试了一下,本地推送没问题,云函数不推送,应该是服务器的问题。可以试一下coolpush
我用天气推送那个,coolpush也不推送到QQ 1241929750 发表于 2021-2-2 20:30
@culprit 楼主,我测试成功了,但是为啥就是不会推送呢
先去server 酱,或者酷推里面测试机器人是否能正常发送信息 顶你了~~~~~~~~ 感谢分享!!! 楼主的部署教程就很好,方便了小白们使用。 感谢分享 顶起来!!! 本帖最后由 411161555 于 2021-2-2 18:03 编辑
谢谢,虽然不用,但是感谢分享精神。 教程挺详细的 放假了我就不仔细看了 ,感谢分享 先mark 回头慢慢看 支持一下大佬,关注了