前言:
每天给你一句天气问候,晚上一句晚安。首要条件,你得有个女朋友。有天气预报,有鸡汤语句。这个例子可以学习的点是Python的请求接口和解析json数据requests,web的微信机器人itchat,定时任务schedule的运用的小例子,Python的语法缩进很严格,一半的错误来自缩进问题。
必要环境:
女朋友或者老婆
需要模块:
Python 2.7+、itchat、requests、schedule
教程:
安装itchatpip install itchat
安装requestspip install requests
安装schedulepip install schedule
代码:#coding:utf-8
import re
import time
import itchat
from itchat.content import *
import schedule
import requests
import sys
reload(sys)
sys.setdefaultencoding('utf-8') #由于我们返回的是中文,Unicode的编码问题,读取文件时使用的编码默认是ascii而不是utf8,所以这里我们要把默认编码设为utf8
# 天气的预报设置
def get_weather(url):
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36'
}
# 请求Weather API并拿到服务器返回的数据
rep = requests.get(url, headers = header)
rep.encoding = "utf-8"
result = ''
weather = rep.text
# 解析服务器返回的数据,具体可参考weather.json文件
index_cityInfo = weather.find("cityInfo")
index_cityId = weather.find("cityId")
index_shidu = weather.find("shidu")
index_pm25 = weather.find("pm25")
index_pm10 = weather.find("pm10")
index_quality = weather.find("quality")
index_wendu = weather.find("wendu")
index_ganmao = weather.find("ganmao")
index_forecast = weather.find("forecast")
index_ymd = weather.find("ymd", index_forecast)
index_week = weather.find("week", index_forecast)
index_sunset = weather.find("sunset", index_forecast)
index_high = weather.find("high", index_forecast)
index_low = weather.find("low", index_forecast)
index_fx = weather.find("fx", index_forecast)
index_fl = weather.find("fl", index_forecast)
index_aqi = weather.find("aqi", index_forecast)
index_type = weather.find("type", index_forecast)
index_notice = weather.find("notice", index_forecast)
# 将解析好的数据组装成想要的格式做为函数的返回值
'''
# 今日天气预报
# 年月日 + 星期 + 所在地城市
# 天气类型 + 风向 + 风力
# 温度范围(最低温度~最高温度)
# 污染指数:PM2.5/PM10/AQI
# 空气质量
# 当前温度 + 空气湿度
# Notice信息
'''
result = '今日天气预报' + '\n' \
+ weather[index_ymd + 6:index_week - 3] + " " \
+ weather[index_week + 7:index_fx - 3] + " " \
+ weather[index_cityInfo + 19:index_cityId - 3] + '\n' \
+ "天气: " + weather[index_type + 7:index_notice - 3] + " " \
+ weather[index_fx + 5:index_fl - 3] \
+ weather[index_fl + 5:index_type - 3] + '\n' \
+ "温度范围:" + weather[index_low + 9:index_sunset - 3] + " ~" \
+ weather[index_high + 10:index_low - 3] + '\n' \
+ "污染指数: PM2.5:" + weather[index_pm25 + 6:index_pm10 - 1] + " " \
+ "PM10:" + weather[index_pm10 + 6:index_quality - 1] + " " \
+ "AQI:" + weather[index_aqi + 5:index_ymd - 2] + '\n' \
+ "空气质量:" + weather[index_quality + 10:index_wendu - 3] + '\n' \
+ "当前温度:" + weather[index_wendu + 8:index_ganmao - 3] + " " \
+ "空气湿度:" + weather[index_shidu + 8:index_pm25 - 3] + '\n' \
+ weather[index_notice + 9:weather.find('}', index_notice) - 1]
return result
# 获取每天一句晚安话语
'''
#
#主要来源木芽一言
#https://xygeng.cn/170.html
#
'''
def get_news():
# 这里是把木芽每日一句中拿过来的信息发送给你朋友
url = "https://api.xygeng.cn/dailywd/api/"
r = requests.get(url)
content = r.json()['txt']
return content
# 早安的设置
def auto_send_morning():
# weather API的URL,此处的城市编号,参看_city.json
#101210101是城市代码
url = 'http://t.weather.sojson.com/api/weather/city/101210101'
# 调用get_weather函数
GW = get_weather(url)
# 发送微信消息
# 填入你朋友的微信备注
# 发送给备注早安晚安所有的用户
# 发送微信消息,中文随意修改
my_friends = itchat.search_friends('早安晚安')
for g in range(0,len(my_friends)):
itchat.send('早上好Y(^o^)Y,新的一天新气象ヽ(✿゚▽゚)ノ。',my_friends[g]['UserName'])
itchat.send(GW,my_friends[g]['UserName'])
# 每隔86400秒(1天),发送1次
# t = Timer(60, auto_send)
# t.start()
# 晚安的设置
def auto_send_evening():
# 发送微信消息
# 填入你朋友的微信备注
# 发送给备注早安晚安所有的用户
# 发送微信消息,中文随意修改
my_friends = itchat.search_friends('早安晚安')
for g in range(0,len(my_friends)):
itchat.send(get_news()+'。晚安咯,愿你好梦!(๑•̀ㅂ•́)و✧(๑•̀ㅂ•́)و✧',my_friends[g]['UserName'])
# 时间设置
def main():
# 早上8点
schedule.every().day.at("08:00").do(auto_send_morning)
# 晚上10点半
schedule.every().day.at("22:30").do(auto_send_evening)
while True:
schedule.run_pending()#确保schedule一直运行
time.sleep(1)
# 登录,仅支持能登录web版的微信号,新申请的账号一般不可以
if __name__ == '__main__':#启动微信自动登录,二维码登录
itchat.auto_login(hotReload=True, enableCmdQR=2)
main()
itchat.run()### unterminated keywords ###
使用:
1、将代码保存为zaoan.py
2、将文件放置目录下
3、配置好环境的服务器linux输入命令:screen -S wechatpython zaoan.py
4、扫码登录
5、直接后台按组合键Ctrl+a+d
6、退出,先找出你创建的screen,然后kill掉,第二行的41493需要自己修改screen -lsscreen -S 41493.wechat -X quit