Python基于腾讯云函数实现天气每日推送

Python 投稿 73000 0 评论

云函数真的是个好东西,搭配消息推送那可玩性还是比较高的。 本来搭配某酱微信推送,但是发现好像没有QQ直观,因为微信要点进去才能看消息内容,所以天气推送就直接使用QQ的消息推送算了。

天气接口API:http://t.weather.itboy.net/api/weather/city/ + 城市代码
城市代码查询入口:https://where.heweather.com/index.html

#!/usr/bin/python3
#coding=utf-8
 
import requests, json
 
spkey = 'xxxxxxxxxxxx'    #https://qmsg.zendee.cn ---> 去这个地址注册,获取key,然后添加绑定机器人
def get_iciba_everyday():
    icbapi = 'http://open.iciba.com/dsapi/'
    eed = requests.get(icbapi)
    bee = eed.json()  #返回的数据
    english = bee['content']
    zh_CN = bee['note']
    str = '【奇怪的知识】\n' + english + '\n' + zh_CN
    return str
 
def main(arg1,arg2):
    try:
        api = 'http://t.weather.itboy.net/api/weather/city/'             #API地址,必须配合城市代码使用
        city_code = '101230508'               #进入https://where.heweather.com/index.html查询你的城市代码
        tqurl = api + city_code
        response = requests.get(tqurl)
        d = response.json()         #将数据以json形式返回,这个d就是返回的json数据
        if(d['status'] == 200):     #当返回状态码为200,输出天气状况
            parent = d["cityInfo"]["parent"] #省
            city = d["cityInfo"]["city"] #市
            update_time = d["time"] #更新时间
            date = d["data"]["forecast"][0]["ymd"] #日期
            week = d["data"]["forecast"][0]["week"] #星期
            weather_type = d["data"]["forecast"][0]["type"] # 天气
            wendu_high = d["data"]["forecast"][0]["high"] #最高温度
            wendu_low = d["data"]["forecast"][0]["low"] #最低温度
            shidu = d["data"]["shidu"] #湿度
            pm25 = str(d["data"]["pm25"]) #PM2.5
            pm10 = str(d["data"]["pm10"]) #PM10
            quality = d["data"]["quality"] #天气质量
            fx = d["data"]["forecast"][0]["fx"] #风向
            fl = d["data"]["forecast"][0]["fl"] #风力
            ganmao = d["data"]["ganmao"] #感冒指数
            tips = d["data"]["forecast"][0]["notice"] #温馨提示
            cpurl = 'https://qmsg.zendee.cn/send/'+spkey               #自己改发送方式,我专门创建了个群来收消息,所以我用的group
            # 天气提示内容
            tdwt = get_iciba_everyday()  + "\n-----------------------------------------" + "\n【今日份天气】\n城市: " + parent + city + \
                   "\n日期: " + date + "\n星期: " + week + "\n天气: " + weather_type + "\n温度: " + wendu_high + " / "+ wendu_low + "\n湿度: " + \
                    shidu + "\nPM25: " + pm25 + "\nPM10: " + pm10 + "\n空气质量: " + quality + \
                   "\n风力风向: " + fx + fl + "\n感冒指数: "  + ganmao + "\n温馨提示: " + tips + "\n更新时间: " + update_time
            print(tdwt)
            data = {
                'msg':tdwt.encode('utf-8')
            }
            requests.post(cpurl,data=data)         #把天气数据转换成UTF-8格式,不然要报错。
    except:
        error = '【出现错误】\n  今日天气推送错误,请检查服务或网络状态!'
        print(error)

Python基于腾讯云函数实现天气每日推送,测试截图:

Python实现腾讯云函数实现每日天气推送

编程笔记 » Python基于腾讯云函数实现天气每日推送

赞同 (103) or 分享 (0)
游客 发表我的评论   换个身份
取消评论

表情
(0)个小伙伴在吐槽