天天早上,上面的通知都会准时的出如今你的微信中,而实现这些功能只须要短短的几行代码,以下:html
1. 首先须要引用 wxpy python 库以下:
from wxpy import *
import requests
import schedule
2. 注册机器人
bot = Bot(cache_path=True,console_qr = 1)
myself = bot.self
bot.enable_puid('wxpy_puid.pkl')
3. 找到你要发送提醒的微信昵称
Lie = bot.friends().search(u'Lie')
4. 定义定时事件
def auto_send():
weather = sendweather('苏州')
Lie.send(weather)
5. 抓取当每天气
def sendweather(city):
header = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Connection': 'keep-alive',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.235'
}
url = 'https://free-api.heweather.com/s6/weather/forecast?location='+city+'&key=key'
PMurl = 'https://free-api.heweather.com/s6/air/now?parameters&location='+city+'&key=key'
lifeurl = 'https://free-api.heweather.com/s6/weather/lifestyle?location='+city+'&key=key'
# 设定超时时间,防止被网站认为是爬虫
timeout = random.choice(range(80, 180))
rep = requests.get(url, headers=header, timeout=timeout)
pm = requests.get(PMurl, headers=header, timeout=timeout)
life = requests.get(lifeurl, headers=header, timeout=timeout)
result = ''
temp = rep.json()
temp = temp['HeWeather6'][0]
update = temp['update']
now = temp['daily_forecast'][0]
nowTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
pm = pm.json()
pm = pm['HeWeather6'][0]
airnow = pm['air_now_city']
life = life.json()
life = life['HeWeather6'][0]
life = life['lifestyle']
result = '早上好,'+city+' ---' + '\n'+ '\n'\
+ ' 今每天气:'+ now['cond_txt_d'] + ' 转 ' + now['cond_txt_n'] + '\n'\
+ ' 今天温度:'+ now['tmp_min'] + '°C ~ ' + now['tmp_max'] + '°C' + '\n'\
+ ' 风向:'+ now['wind_dir'] + ' ' + now['wind_sc'] + '级 '+ now['wind_spd'] + '千米/小时'+ '\n'\
+ ' 相对湿度:'+ now['hum'] + '%' + '\n'\
+ ' 降水量:'+ now['pcpn'] + 'ml' + ',降水几率:'+ now['pop'] + '%' + '\n'\
+ ' 能见度:'+ now['vis'] + '千米' + '\n'\
+ '------------------------------------------' + '\n'\
+ '今天空气质量:'+'\n'\
+ ' 空气质量指数:'+ airnow['aqi']+'\n'\
+ ' 主要污染物:'+ airnow['main']+'\n'\
+ ' 空气质量:'+ airnow['qlty']+'\n'\
+ ' 二氧化氮指数:'+ airnow['no2']+'\n'\
+ ' 二氧化硫指数:'+ airnow['so2']+'\n'\
+ ' 一氧化碳指数:'+ airnow['co']+'\n'\
+ ' pm10指数:'+ airnow['pm10']+'\n'\
+ ' pm25指数:'+ airnow['pm25']+'\n'\
+ ' 臭氧指数:'+ airnow['o3'] +'\n'\
+ '------------------------------------------' + '\n'\
+ '一、'+ life[0]['txt']+'\n\n'\
+ '二、'+ life[1]['txt']+'\n\n'\
+ '三、'+ life[2]['txt']+'\n\n'\
+ '😄😊😉😍😘😚😜😝😳😁'+'\n\n'\
result = result + '发送时间:' + nowTime + '\n'
return result
6. 设置定时发送事件
schedule.every().day.at("07:30").do(auto_send)
7. 将机器人事件注册
while True:
schedule.run_pending()
time.sleep(1)
复制代码
到这里就大功告成了!! 机器人将在天天早上七点的时候推送天气预报,到你心爱的TA的微信上。python
由于在wxpy中已经继承了图灵机器人,咱们只用设置本身申请的key就能够了。web
tuling = Tuling(api_key='key')
@bot.register(msg_types=FRIENDS)
复制代码
只须要两行代码便可,注册一个定时执行事件,写好提早想好的提醒语。json
def water(msg):
qc = bot.friends().search(u'★淡忘★')[0]
qc.send(msg)
schedule.every().day.at("10:00").do(water, '你的小可爱提醒你:'+ '\n\n' + '该喝水了!!!!快喝一杯水!!!!😘😘😘😘😘😘😘')
schedule.every().day.at("11:30").do(water, '你的小可爱提醒你:'+ '\n\n' + '吃饭时间到了,准备去吃饭吧!中午记得走走锻炼身体,活动一下!!!😘😘😘😘😘😘😘')
复制代码
例如:天天下午两天推送当天NBA赛况,固然你也能够主动查询。
例如:天天下午四点推送一篇短文阅读
例如:推送当天黄历信息
等等
。。。
你还能够为TA作的更多。
复制代码