码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-11-13 16:10:16      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:环境   派生类   city   com   消息   color   第一个   查询   dao   

前言

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

作者: PK哥

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

http://note.youdao.com/noteshare?id=3054cce4add8a909e784ad934f956cef

如今,对于我们年轻人来说,获取天气情况很方便,但是对于我们不擅长用手机的父母来说,还是很吃力,他们用的多的还是微信吧。为此,我用不到 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()

 

异常处理

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

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

 

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

部署在服务器

在服务器中运行程序,直接扫码登录微信,下面是我周五开始运行的,相隔 24 小时后,周六再次调用方法,获取新的天气信息。

技术图片

发到群里效果如下图,当然,你还可以多加一些接口返回的信息或者自己想说的话

 技术图片

可能存在的问题

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

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

群名称没错的话,看看自动发送信息的这个号有没有把这个群添加到通讯录。

技术图片

这样,一个定时发送消息的小工具就完成了,你也可以在上面扩展,加上其他功能,这样就更完善了。

完整代码

  • jinshan-message.py

 1 from twilio.rest import Client
 2 import requests
 3 from threading import Timer
 4 from time import sleep
 5 ?
 6 # def get_weather():
 7 #     url = ‘http://v.juhe.cn/weather/index?cityname=上海&key=b0da46b36d3a2cce53fac9cdf51dc98a‘   # 城市名cityname和key值换成自己的
 8 #     weather_json = requests.get(url).json()
 9 #     temperature = weather_json[‘result‘][‘today‘][‘temperature‘]
10 #     weather = weather_json[‘result‘][‘today‘][‘weather‘]
11 #     week = weather_json[‘result‘][‘today‘][‘week‘]
12 #     city = weather_json[‘result‘][‘today‘][‘city‘]
13 #     dressing_advice = weather_json[‘result‘][‘today‘][‘dressing_advice‘]
14 #     return temperature, weather, week, city, dressing_advice
15 ?
16 def get_msg():
17     url = http://open.iciba.com/dsapi/   # 金山词霸每日一句 api 链接
18     html = requests.get(url)
19     content = html.json()[content]  # 获取每日一句英文语句
20     note = html.json()[note]        # 获取每日一句英文的翻译语句
21     return content, note
22 ?
23 ?
24 while True:
25     try:
26         content, note = get_msg()
27         msg_all = content + \n + note + \n + from 爱你的人
28         # Your Account Sid and Auth Token from twilio.com/console
29         # DANGER! This is insecure. See http://twil.io/secure
30         account_sid = 输入你的account_sid
31         auth_token = 输入你的auth_token
32         client = Client(account_sid, auth_token)
33         message = client.messages 34             .create(body=msg_all, from_=+输入你获得的免费号码, to=+输入你验证的接收号码)
35         print(message.sid)
36         print(msg_all)
37 ?
38         t = Timer(86400, get_msg)  # Timer(定时器)是 Thread 的派生类,用于在指定时间后调用一个方法。
39         t.start()
40         t.join()
41     # 异常处理,发送失败
42     except:
43         print(发送失败)
44         break

 

parent-weather.py

 1 from wxpy import *
 2 import requests
 3 from threading import Timer
 4 from time import sleep
 5 ?
 6 bot = Bot(cache_path=True)  # 扫码登录微信,如果在Linux环境中运行,加一个参数 bot = Bot(console_qr=-2,cache_path=True)
 7 ?
 8 ?
 9 def get_weather():
10     url = http://v.juhe.cn/weather/index?cityname=上海&key=xxx  # 城市名cityname和key值换成自己的
11     weather_json = requests.get(url).json()
12     temperature = weather_json[result][today][temperature]
13     weather = weather_json[result][today][weather]
14     week = weather_json[result][today][week]
15     city = weather_json[result][today][city]
16     dressing_advice = weather_json[result][today][dressing_advice]
17     return temperature, weather, week, city, dressing_advice
18 ?
19 ?
20 while True:   # !!!调试时记得先把while True注释掉,不然会一直重复发送失败,一天限制100次调用的,成功后再加上注释
21     try:
22         temperature, weather, week, city, dressing_advice = get_weather()
23         # 发送到微信群里
24         my_groups = bot.groups().search(你的群名称)[0]   # 换成发送信息的群名称
25         msg = 今天是: + week + \n 26               + city + 的天气: + weather + \n 27               + 今天温度: + temperature +\n 28               + 穿衣指南: + dressing_advice
29         print(msg)
30         my_groups.send(msg)
31 ?
32         # 单独私发微信
33         # my_friend = bot.friends().search(u‘pk‘)[0]  # 此处是对方自己的昵称,不是微信号,也不是你的备注。
34         # my_friend.send(msg)  # 发送文字
35 ?
36         t = Timer(86400, get_weather)  # Timer(定时器)是 Thread 的派生类,用于在指定时间后调用一个方法。
37         t.start()
38         t.join()
39     # 异常处理,发送失败,发送提醒消息给自己
40     except BaseException:
41         my_friend = bot.friends().search(uxxx)[
42             0]  # 发送不成功,则发送消息给自己,提醒消息发送失败 xxx改成你自己微信的昵称
43         my_friend.send(u天气消息发送失败,请停止程序进行调试)
44         break

 

  • weather-message.py

 1 from twilio.rest import Client
 2 import requests
 3 from threading import Timer
 4 from time import sleep
 5 ?
 6 def get_weather():
 7     url = http://v.juhe.cn/weather/index?cityname=上海&key=输入你自己的key,在v.juhe.cn网站注册获取  # 城市名cityname和key值换成自己的
 8     weather_json = requests.get(url).json()
 9     temperature = weather_json[result][today][temperature]
10     weather = weather_json[result][today][weather]
11     week = weather_json[result][today][week]
12     city = weather_json[result][today][city]
13     dressing_advice = weather_json[result][today][dressing_advice]
14     return temperature, weather, week, city, dressing_advice
15 ?
16 ?
17 while True:
18     try:
19         temperature, weather, week, city, dressing_advice = get_weather()
20 ?
21         msg = 今天是: + week + \n 22               + city + 的天气: + weather + \n 23               + 今天温度: + temperature +\n 24               + 穿衣指南: + dressing_advice
25         print(msg)
26 ?
27         # Your Account Sid and Auth Token from twilio.com/console
28         # DANGER! This is insecure. See http://twil.io/secure
29         account_sid = 输入你的account_sid
30         auth_token = 输入你的auth_token
31         client = Client(account_sid, auth_token)
32 ?
33         message = client.messages 34             .create(body=msg, from_=输入你获取的号码, to=+输入你验证过的号码)
35         print(message.sid)
36 ?
37         t = Timer(86400, get_weather)  # Timer(定时器)是 Thread 的派生类,用于在指定时间后调用一个方法。
38         t.start()
39         t.join()
40     # 异常处理,发送失败
41     except BaseException:
42         print(发送失败)
43         break

 

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

标签:环境   派生类   city   com   消息   color   第一个   查询   dao   

原文地址:https://www.cnblogs.com/qun821460695/p/11849880.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!