天天早上,上面的通知都會準時的出如今你的微信中,而實現這些功能只須要短短的幾行代碼,以下: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作的更多。
複製代碼