吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 13576|回复: 98
收起左侧

[Python 转载] 【修改版】腾讯云函数实现每日天气推送

  [复制链接]
culprit 发表于 2020-9-20 14:03
本帖最后由 culprit 于 2021-4-8 13:14 编辑

云函数推送请看原帖

原帖子地址:https://www.52pojie.cn/thread-1210880-1-1.html 原帖作者:@黑蚁


天气接口API:http://t.weather.itboy.net/api/weather/city/ + 城市代码

城市代码查询入口:https://where.heweather.com/index.html


本帖对@黑蚁 的代码进行修改,添加try/except捕获异常,对原来的多余代码进行精简,添加PM25、PM10和感冒指数推送。


代码如下:
[Python] 纯文本查看 复制代码
#!/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)


测试截图:

测试截图

测试截图




免费评分

参与人数 6吾爱币 +5 热心值 +4 收起 理由
亲亲卡农 + 1 + 1 谢谢@Thanks!
WJF12321 + 1 用心讨论,共获提升!
jawy + 1 我很赞同!
padastar + 1 + 1 我很赞同!
schedule + 1 我很赞同!
从零学习的歪经小 + 1 + 1 挺好,我上次也弄了个云函数,放了个钉钉机器人。天天发舔狗日记【滑稽】

查看全部评分

本帖被以下淘专辑推荐:

  • · 雲方|主题: 31, 订阅: 25

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

头像被屏蔽
Menguy 发表于 2020-9-20 14:22
提示: 作者被禁止或删除 内容自动屏蔽
Victor-rich 发表于 2020-9-20 18:09
楼主这个是什么意思啊
icbapi = 'api'  #下载.PY文件可见
这个api在哪啊
南岸 发表于 2020-9-20 14:10
AniMe1235 发表于 2020-9-20 14:11
厉害了,,,,,,
YIHAN1008 发表于 2020-9-20 14:48
学习了学习了
虚空先森 发表于 2020-9-20 15:10
spkey = 'xxxxxxxxxxxxxxxxxxx'    #CPkey,填上自己的
怎么获取
郭嘉的不要抢 发表于 2020-9-20 15:23
大神能不能帮忙配置成企业微信的机器人的语言,我直接复制过去,小白完全看不懂呀
18924668205 发表于 2020-9-20 15:27
Traceback (most recent call last):
TypeError: main() takes 0 positional arguments but 2 were given
凡人i 发表于 2020-9-20 15:54
大佬qq那个怎么发的?机器人么?
 楼主| culprit 发表于 2020-9-20 16:13
虚空先森 发表于 2020-9-20 15:10
spkey = 'xxxxxxxxxxxxxxxxxxx'    #CPkey,填上自己的
怎么获取

下载代码文件,里面有网址哦,登录获取自己的就行
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-6 02:45

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表