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

前言html

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

做者: PK哥api

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=您申请的KEYurl

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(u'brucepk')[
3             0]  # 发送不成功,则发送消息给本身,提醒消息发送失败
4         my_friend.send(u'天气消息发送失败')

 

咱们的程序须要持续运行,那是否是须要一直在电脑上运行啊,这样有点不现实啊,咱们把它部署到服务器上就能够搞定了,如今服务器也很便宜,作活动的时候通常 100 元之内就能够买一年。

部署在服务器

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

在这里插入图片描述

发到群里效果以下图,固然,你还能够多加一些接口返回的信息或者本身想说的话

 在这里插入图片描述

可能存在的问题

一、个人微信登陆不了网页版微信 由于 itchat 库和 wxpy 库都是调用微信的网页版接口,若是你的微信注册比较晚,被限制了网页版登陆功能,那这个程序你没法运行。

二、发送不到指定群 先检查下群名称,把群名称一些 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(u'xxx')[
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
相关文章
相关标签/搜索