天气变冷了,给你的爱人制作一个天气提醒小助手

前言

文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。

作者: PK哥

PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取

python免费学习资料以及群交流解答点击即可加入


如今,对于我们年轻人来说,获取天气情况很方便,但是对于我们不擅长用手机的父母来说,还是很吃力,他们用的多的还是微信吧。为此,我用不到 40 行代码写了一个小工具,每天定时把当天的天气情况直接发到微信群里。

查询天气接口

要获取天气情况,需要一个查询天气的接口,网上找了一下,一般都是注册后送一定调用次数的,我选择了一个,免费送 500 次查询次数的。
在这里插入图片描述
我们看看接口的 API 文档。
在这里插入图片描述
其中城市名 cityname 和 key 是必填项。

http://v.juhe.cn/weather/index?format=2&cityname=%E8%8B%8F%E5%B7%9E&key=您申请的KEY

key 值在 juhe.cn 我的接口那一栏中可以看到。
在这里插入图片描述
我们也可以事先在 Postman 工具中看看接口能否调通。
在这里插入图片描述
Postman 接口工具没用过的没关系,他们网页上也提供了调试工具。​
在这里插入图片描述

查询天气方法

我们直接用 requests 库请求接口就能获得 json 格式的天气信息,json 数据中包含了当天和未来几天的天气信息,这里我只需要当天的,当天数据都在 result 下的 today 里,提取出来用 return 返回。
在这里插入图片描述

通过微信发送天气消息

我们通过微信把天气信息发到群里,这里我们需要用到调用微信的库,itchat 库或者 wxpy 都可以,这里我用了 wxpy 库。

先导入 wxpy 库。

from wxpy import *

我们把刚才的请求接口返回的天气信息整合一下,然后用 wxpy 库的 search 方法通过群名称找到你需要发送天气消息的这个群,用 send 方法发送。
在这里插入图片描述
我这里是发送到群里,当然,你也可以直接发送给个人微信。

my_friend = bot.friends().search(u'pk哥')[0]

定时操作

如果每次都需要我们手动运行,那就有点麻烦,我们要让程序每天在固定时间定时发送,这里需要用到 Timer 定时器。

Timer 定时器格式:

Timer(86400, get_weather)

第一个参数表示相隔指定时间(单位:秒)后再次调用方法(第二个参数),注意,方法后不要带括号。

86400 秒就是相隔 24 小时,也就是一天的时间。

t = Timer(86400, get_weather)  
t.start()
t.join()

异常处理

如果信息发送失败,我们把信息发给自己,这里我做了一个异常处理。

   except BaseException:
        my_friend = bot.friends().search(u'brucepk')[
            0]  # 发送不成功,则发送消息给自己,提醒消息发送失败
        my_friend.send(u'天气消息发送失败')

我们的程序需要持续运行,那是不是需要一直在电脑上运行啊,这样有点不现实啊,我们把它部署到服务器上就可以搞定了,现在服务器也很便宜,做活动的时候一般 100 元以内就可以买一年。

部署在服务器

在服务器中运行程序,直接扫码登录微信,下面是我周五开始运行的,相隔 24 小时后,周六再次调用方法,获取新的天气信息。
在这里插入图片描述
发到群里效果如下图,当然,你还可以多加一些接口返回的信息或者自己想说的话。
在这里插入图片描述

可能存在的问题

1、我的微信登录不了网页版微信
因为 itchat 库和 wxpy 库都是调用微信的网页版接口,如果你的微信注册比较晚,被限制了网页版登录功能,那这个程序你无法运行。

2、发送不到指定群
先检查下群名称,把群名称一些 emoji 表情符号去掉,这些特殊符号可能导致无法识别。

群名称没错的话,看看自动发送信息的这个号有没有把这个群添加到通讯录。
在这里插入图片描述
这样,一个定时发送消息的小工具就完成了,你也可以在上面扩展,加上其他功能,这样就更完善了。

完整代码

  • jinshan-message.py
from twilio.rest import Client
import requests
from threading import Timer
from time import sleep

# def get_weather():
#     url = 'http://v.juhe.cn/weather/index?cityname=上海&key=b0da46b36d3a2cce53fac9cdf51dc98a'   # 城市名cityname和key值换成自己的
#     weather_json = requests.get(url).json()
#     temperature = weather_json['result']['today']['temperature']
#     weather = weather_json['result']['today']['weather']
#     week = weather_json['result']['today']['week']
#     city = weather_json['result']['today']['city']
#     dressing_advice = weather_json['result']['today']['dressing_advice']
#     return temperature, weather, week, city, dressing_advice

def get_msg():
    url = 'http://open.iciba.com/dsapi/'   # 金山词霸每日一句 api 链接
    html = requests.get(url)
    content = html.json()['content']  # 获取每日一句英文语句
    note = html.json()['note']        # 获取每日一句英文的翻译语句
    return content, note


while True:
    try:
        content, note = get_msg()
        msg_all = content + '\n' + note + '\n' + 'from 爱你的人'
        # Your Account Sid and Auth Token from twilio.com/console
        # DANGER! This is insecure. See http://twil.io/secure
        account_sid = '输入你的account_sid'
        auth_token = '输入你的auth_token'
        client = Client(account_sid, auth_token)
        message = client.messages \
            .create(body=msg_all, from_='+输入你获得的免费号码', to='+输入你验证的接收号码')
        print(message.sid)
        print(msg_all)

        t = Timer(86400, get_msg)  # Timer(定时器)是 Thread 的派生类,用于在指定时间后调用一个方法。
        t.start()
        t.join()
    # 异常处理,发送失败
    except:
        print('发送失败')
        break

parent-weather.py

from wxpy import *
import requests
from threading import Timer
from time import sleep

bot = Bot(cache_path=True)  # 扫码登录微信,如果在Linux环境中运行,加一个参数 bot = Bot(console_qr=-2,cache_path=True)


def get_weather():
    url = 'http://v.juhe.cn/weather/index?cityname=上海&key=xxx'  # 城市名cityname和key值换成自己的
    weather_json = requests.get(url).json()
    temperature = weather_json['result']['today']['temperature']
    weather = weather_json['result']['today']['weather']
    week = weather_json['result']['today']['week']
    city = weather_json['result']['today']['city']
    dressing_advice = weather_json['result']['today']['dressing_advice']
    return temperature, weather, week, city, dressing_advice


while True:   # !!!调试时记得先把while True注释掉,不然会一直重复发送失败,一天限制100次调用的,成功后再加上注释
    try:
        temperature, weather, week, city, dressing_advice = get_weather()
        # 发送到微信群里
        my_groups = bot.groups().search('你的群名称')[0]   # 换成发送信息的群名称
        msg = '今天是:' + week + '\n' \
              + city + '的天气:' + weather + '\n' \
              + '今天温度:' + temperature +'\n' \
              + '穿衣指南:' + dressing_advice
        print(msg)
        my_groups.send(msg)

        # 单独私发微信
        # my_friend = bot.friends().search(u'pk')[0]  # 此处是对方自己的昵称,不是微信号,也不是你的备注。
        # my_friend.send(msg)  # 发送文字

        t = Timer(86400, get_weather)  # Timer(定时器)是 Thread 的派生类,用于在指定时间后调用一个方法。
        t.start()
        t.join()
    # 异常处理,发送失败,发送提醒消息给自己
    except BaseException:
        my_friend = bot.friends().search(u'xxx')[
            0]  # 发送不成功,则发送消息给自己,提醒消息发送失败 xxx改成你自己微信的昵称
        my_friend.send(u'天气消息发送失败,请停止程序进行调试')
        break
  • weather-message.py
from twilio.rest import Client
import requests
from threading import Timer
from time import sleep

def get_weather():
    url = 'http://v.juhe.cn/weather/index?cityname=上海&key=输入你自己的key,在v.juhe.cn网站注册获取'  # 城市名cityname和key值换成自己的
    weather_json = requests.get(url).json()
    temperature = weather_json['result']['today']['temperature']
    weather = weather_json['result']['today']['weather']
    week = weather_json['result']['today']['week']
    city = weather_json['result']['today']['city']
    dressing_advice = weather_json['result']['today']['dressing_advice']
    return temperature, weather, week, city, dressing_advice


while True:
    try:
        temperature, weather, week, city, dressing_advice = get_weather()

        msg = '今天是:' + week + '\n' \
              + city + '的天气:' + weather + '\n' \
              + '今天温度:' + temperature +'\n' \
              + '穿衣指南:' + dressing_advice
        print(msg)

        # Your Account Sid and Auth Token from twilio.com/console
        # DANGER! This is insecure. See http://twil.io/secure
        account_sid = '输入你的account_sid'
        auth_token = '输入你的auth_token'
        client = Client(account_sid, auth_token)

        message = client.messages \
            .create(body=msg, from_='输入你获取的号码', to='+输入你验证过的号码')
        print(message.sid)

        t = Timer(86400, get_weather)  # Timer(定时器)是 Thread 的派生类,用于在指定时间后调用一个方法。
        t.start()
        t.join()
    # 异常处理,发送失败
    except BaseException:
        print('发送失败')
        break
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值