詳解用python自制微信機器人,定時發(fā)送天氣預報
0 引言
前段時間找到了一個免費的天氣預報API,費了好段時間把這個API解析并組裝成自己想用的格式了,就想著如何實現(xiàn)每天發(fā)送天氣信息給自己。最近無意中發(fā)現(xiàn)了wxpy庫,用它來做再合適不過了。以下是wxpy庫的簡介:
wxpy基于itchat,使用了 Web 微信的通訊協(xié)議,通過大量接口優(yōu)化提升了模塊的易用性,并進行豐富的功能擴展。實現(xiàn)了微信登錄、收發(fā)消息、搜索好友、數(shù)據(jù)統(tǒng)計、微信公眾號、微信好友、微信群基本信息獲取等功能。
廢話不多說,代碼寫起來。
1 環(huán)境
操作系統(tǒng):Windows / Linux
Python版本:3.7.2
2 代碼實現(xiàn)
我們要實現(xiàn)用Python來發(fā)微信,發(fā)送的內(nèi)容是每天最新的天氣信息。很明顯我們需要完成兩部分的準備,先來看看獲取天氣信息這部分內(nèi)容。
2.0 準備工作
本文我們用到的第三方庫有requests、wxpyy,若環(huán)境還沒有,按如下方式進行安裝即可。
pip install wxpy
pip install requests
2.1 獲取天氣信息
這里我使用的API的請求鏈接如下:
http://t.weather.sojson.com/api/weather/city/city_code
請求方式是GET方法,使用時注意更換為自己城市對應的city_code,除此之外不用帶任何參數(shù)。
請求是restfull風格,city_code為9位數(shù)字,如下示例:
{
"_id": 58,
"id": 59,
"pid": 3,
"city_code": "101230201",
"city_name": "廈門"
}
大家可以從_city.json文件中獲取各個城市對應的編號。該文件我已經(jīng)放在Github本文章對應的目錄下了,大家可自行查詢使用。
# weather API的URL,此處的城市編號,參看_city.json
url = 'http://t.weather.sojson.com/api/weather/city/101010200'
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并拿到服務器返回的數(shù)據(jù)
rep = requests.get(url, headers = header)
rep.encoding = "utf-8"
result = ''
weather = rep.tex
這個API接口的返回值內(nèi)容很多,以下僅展示返回的部分信息。實際使用中僅用到三塊內(nèi)容,首先是城市信息。
"cityInfo": {
"city": "海淀區(qū)", //請求城市
"cityId": "101010200", //城市ID
"parent": "北京市", //上級,一般是省份
"updateTime": "09:02" //天氣更新時間
}
其次是,該城市當前天氣的空氣相關指數(shù)。
"data": {
"shidu": "32%", //濕度
"pm25": 35.0, //pm2.5
"pm10": 97.0, //pm10
"quality": "良", //空氣質(zhì)量
"wendu": "7", //溫度
"ganmao": "極少數(shù)敏感人群應減少戶外活動", //感冒提醒(指數(shù))
}
第三部分,該城市當前天氣的溫度風力等另外一些指數(shù)。
"forecast": [ //今天+未來14天
{
"date": "16", //日期
"sunrise": "06: 28",
"high": "高溫 20.0℃",
"low": "低溫 2.0℃",
"sunset": "18: 21",
"aqi": 48.0,
"ymd": "2019-03-16", //年月日
"week": "星期六",
"fx": "西北風", //風向
"fl": "3-4級", //風力
"type": "晴",
"notice": "愿你擁有比陽光明媚的心情"
}
]
注:這個API接口返回值完整的示例,請見Github中本文章目錄下的weather.json文件。
拿到返回值之后,需要解析,并轉(zhuǎn)換組裝成我們想要的格式。
# 解析服務器返回的數(shù)據(jù),具體可參考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)
這是我最終想達到的效果如下:
# 今日天氣預報
# 年月日 + 星期 + 所在地城市
# 天氣類型 + 風向 + 風力
# 溫度范圍(最低溫度~最高溫度)
# 污染指數(shù):PM2.5/PM10/AQI
# 空氣質(zhì)量
# 當前溫度 + 空氣濕度
# Notice信息
轉(zhuǎn)換化具體代碼就是這樣子的:
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' \
+ "污染指數(shù): 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' \
+ "空氣質(zhì)量:" + 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]
這樣我們的第一步,獲取天氣信息就完成了。接下來就是登錄微信定時發(fā)送消息了。
2.2 登錄微信定時發(fā)送消息
首先要登錄微信,一行代碼就搞定了。這里實際上是掃二維碼登錄了一個Web版的微信。
# 初始化機器人,掃碼登陸微信,適用于Windows系統(tǒng) bot = Bot() # Linux系統(tǒng),執(zhí)行登陸請調(diào)用下面的這句 bot = Bot(console_qr=2, cache_path="botoo.pkl")
然后我們需要定義一個發(fā)送消息的函數(shù),將獲取并解析好的天氣信息發(fā)送給指定微信好友。
# 調(diào)用get_weather函數(shù) GW = get_weather() # 填入你朋友的微信昵稱,注意這里不是備注,也不是微信帳號 my_friend = bot.friends().search(u'一個昵稱')[0] # 發(fā)送微信消息 my_friend.send(u"早上好Y(^o^)Y,這里是今日份的天氣信息請查收!") my_friend.send(GW) my_friend.send(u"Have a Nice Day!") # 每隔86400秒(1天),發(fā)送1次 t = Timer(86400, auto_send) t.start()
接下來,你可以使用try...except...語句來實現(xiàn)在消息失敗時發(fā)出告警:
try:
'''此處為發(fā)送消息的代碼,即上一段內(nèi)容'''
except:
# 你的微信昵稱,注意這里不是備注,也不是微信帳號
my_friend = bot.friends().search('&嫻敲棋子&')[0]
my_friend.send(u"報告老板,今日份的信息發(fā)送失敗了!")
最后運行主函數(shù),調(diào)用發(fā)送消息的函數(shù)即可。
# 調(diào)用函數(shù)進行消息發(fā)送 auto_send()
3 效果展示
這是我清晨收到的微信消息截圖,看上去還不錯。沒白忙活😉

4 后記
我把這個腳本丟在了我的樹莓上,掛在后臺一直運行,簡直完美。
這里僅是實現(xiàn)一個最簡單的定時發(fā)送,后續(xù)考慮如何實現(xiàn)多個時間點的定時發(fā)送,還準備加上早間新聞資訊以及火車放票信息等內(nèi)容。
以上所述是小編給大家介紹的用python自制微信機器人,定時發(fā)送天氣預報詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
Python中使用Queue和Condition進行線程同步的方法
這篇文章主要介紹了Python中使用Queue模塊和Condition對象進行線程同步的方法,配合threading模塊下的線程編程進行操作的實例,需要的朋友可以參考下2016-01-01
python+VTK環(huán)境搭建及第一個簡單程序代碼
這篇文章主要介紹了python+VTK環(huán)境搭建及第一個簡單程序代碼,簡單介紹了vtk,然后分享了安裝步驟,最后涉及一個簡單的代碼示例,具有一定借鑒價值,需要的朋友可以參考下。2017-12-12

