【pyhon笔记】Qmsg酱qq推送天气情况
本帖最后由 DestinyW 于 2021-3-6 11:08 编辑import requests
import json
webhook = 'https://qmsg.zendee.cn/send/你获取的key' # Qmsg酱接口
api_url = 'https://tianqiapi.com/api' # 免费天气接口
city = '城市名字'
weatherParams = {
'appid': '37824871', # 必填,也可以填自己注册的
'appsecret': 'aY5w4uXK', # 必填,也可以填自己注册的
'version': 'v6', # 必填
'city': city, # 城市代码,名字,ip三选一,默认ip
}
def main_handler(url, params):
dataList = [] # 创建空列表
res = json.loads(requests.get(url, params).text) # 提交post
city = res['city'] # 城市名字
date = res['date'] # 当前日期
week = res['week'] # 当前星期
update = res['update_time'] # 更新时间
wea = res['wea'] # 天气情况
tem = res['tem'] # 实时温度
tem1 = res['tem1'] # 温度上限
tem2 = res['tem2'] # 温度下限
hunidity = res['humidity'] # 湿度
win = res['win'] # 风向
win_speed = res['win_speed'] # 风力等级
visibility = res['visibility']# 能见度
air = res['air'] # 空气质量
air_level = res['air_level'] # 空气质量等级
air_tips = res['air_tips'] # 空气质量描述
dataList.extend(
[city, date, week, update, wea, tem, tem1, tem2, hunidity, win, win_speed, visibility, air, air_level,
air_tips]) # 向空列表中追加天气内容
QQPusher(dataList)
def QQPusher(dataList):
data = {
'msg': '今日天气推送🍀 \n---\n城市:{}\n日期:{}\n星期:{}\n更新日期:{}\n---\n天气情况:{}\n 实时温度:{}℃, 温度范围:{}/{}℃\n湿度:{}\n---\n风向{}\n风力等级:{}\n能见度:{}\n---\n空气质量:{}\n空气质量等级:{}\n空气质量描述:{}\n温馨提示:疫情期间,外出请佩戴口罩!'.format(
dataList, dataList, dataList, dataList, dataList, dataList, dataList, dataList,
dataList, dataList, dataList, dataList, dataList, dataList, dataList)
}
requests.post(webhook, data)
main_handler(api_url, weatherParams)
测试截图:自己改一下那个qmsg酱的接口链接就行了,需要注册并添加好友
可以放到腾讯云函数里面,亲测可用 夕尘幽兰 发表于 2021-3-12 11:33
有没有server酱推送的版本啊?感谢分享
接口换成server的链接,消息格式换成server的格式就行了
# -*- coding: utf8 -*-
import requests
import json
qq = 'https://qmsg.zendee.cn/send/'# Qmsg酱接口
wechat = 'https://sc.ftqq.com/.send' # server酱接口
api_url = 'https://tianqiapi.com/api'# 免费天气接口
yourcity = '东莞'
weatherParams = {
'appid': '37824871',# 必填
'appsecret': 'aY5w4uXK',# 必填
'version': 'v6',# 必填
'city': yourcity,# 城市代码,名字,ip三选一,默认ip
}
def getApi(url, params):
dataList = []# 创建空列表
res = json.loads(requests.get(url, params).text)# 提交post
city = res['city']# 城市名字
date = res['date']# 当前日期
week = res['week']# 当前星期
update = res['update_time']# 更新时间
wea = res['wea']# 天气情况
tem = res['tem']# 实时温度
tem1 = res['tem1']# 温度上限
tem2 = res['tem2']# 温度下限
hunidity = res['humidity']# 湿度
win = res['win']# 风向
win_speed = res['win_speed']# 风力等级
visibility = res['visibility']# 能见度
air = res['air']# 空气质量
air_level = res['air_level']# 空气质量等级
air_tips = res['air_tips']# 空气质量描述
dataList.extend(
[city, date, week, update, wea, tem, tem1, tem2, hunidity, win, win_speed, visibility, air, air_level,
air_tips])# 向空列表中追加天气内容
QQPusher(dataList)
def QQPusher(dataList):
data1 = {
'msg': '今日天气推送🍀 \n---\n城市:{}\n日期:{}\n星期:{}\n更新日期:{}\n---\n天气情况:{}\n 实时温度:{}℃, 温度范围:{}/{}℃\n湿度:{}\n---\n风向{}\n风力等级:{}\n能见度:{}\n---\n空气质量:{}\n空气质量等级:{}\n空气质量描述:{}\n温馨提示:疫情期间,外出请佩戴口罩!'.format(
dataList, dataList, dataList, dataList, dataList, dataList, dataList, dataList,
dataList, dataList, dataList, dataList, dataList, dataList, dataList)
}
data2 = {
'text': '东莞市天气状况',
'desp': '今日天气推送🍀 \n---\n城市:{}\n日期:{}\n星期:{}\n更新日期:{}\n---\n天气情况:{}\n 实时温度:{}℃, 温度范围:{}/{}℃\n湿度:{}\n---\n风向{}\n风力等级:{}\n能见度:{}\n---\n空气质量:{}\n空气质量等级:{}\n空气质量描述:{}\n温馨提示:疫情期间,外出请佩戴口罩!'.format(
dataList, dataList, dataList, dataList, dataList, dataList, dataList, dataList,
dataList, dataList, dataList, dataList, dataList, dataList, dataList)
}
requests.post(qq, data1) # qq推送
requests.post(wechat, data2) #微信推送
getApi(api_url, weatherParams)
DestinyW 发表于 2021-3-13 08:20
因为腾讯云函数那里上面那个执行方法默认是main_handler,你只需要把两个getApi替换成main_handler就行了
换了之后是这个报错:
{"errorCode":-1,"errorMessage":"user code exception caught","stackTrace":"Traceback (most recent call last):\nFile \"/var/user/index.py\", line 23, in main_handler\n res = json.loads(requests.get(url, params).text)# 提交post\nFile \"/var/lang/python3/lib/python3.6/site-packages/requests/api.py\", line 75, in get\n return request('get', url, params=params, **kwargs)\nFile \"/var/lang/python3/lib/python3.6/site-packages/requests/api.py\", line 60, in request\n return session.request(method=method, url=url, **kwargs)\nFile \"/var/lang/python3/lib/python3.6/site-packages/requests/sessions.py\", line 533, in request\n resp = self.send(prep, **send_kwargs)\nFile \"/var/lang/python3/lib/python3.6/site-packages/requests/sessions.py\", line 640, in send\n adapter = self.get_adapter(url=request.url)\nFile \"/var/lang/python3/lib/python3.6/site-packages/requests/sessions.py\", line 731, in get_adapter\n raise InvalidSchema(\"No connection adapters were found for '%s'\" % url)\nrequests.exceptions.InvalidSchema: No connection adapters were found for '{'key1': 'test value 1', 'key2': 'test value 2'}'","statusCode":430} 啊?萌新第一次发帖不知道怎么弄代码{:1_907:} DestinyW 发表于 2021-2-27 14:27
啊?萌新第一次发帖不知道怎么弄代码
<>这个图标就是了 云函数怎么弄能简单说一下吗 大哥,代码格式能整理一下不...都跑不出来 谢谢分享 感谢分享 fake 发表于 2021-2-27 14:55
云函数怎么弄能简单说一下吗
就点新建,然后在线编辑就行了 yu17863200980 发表于 2021-2-27 15:18
大哥,代码格式能整理一下不...都跑不出来
现在可以了吗 {"errorCode":-1,"errorMessage":"Traceback (most recent call last):\nFile \"/var/runtime/python3/bootstrap.py\", line 133, in init_handler\n func_handler = get_func_handler(file.rsplit(\".\", 1), func)\nFile \"/var/runtime/python3/bootstrap.py\", line 160, in get_func_handler\n return getattr(mod, fname)\nAttributeError: module 'index' has no attribute 'main_handler'","requestId":"5eb5bac9-ba69-4f98-a48c-0d04d2125450","statusCode":443}
我本地使用pycharm可以运行,放到腾讯云函数报以上错误