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

4 后記
我把這個(gè)腳本丟在了我的樹(shù)莓上,掛在后臺(tái)一直運(yùn)行,簡(jiǎn)直完美。
這里僅是實(shí)現(xiàn)一個(gè)最簡(jiǎn)單的定時(shí)發(fā)送,后續(xù)考慮如何實(shí)現(xiàn)多個(gè)時(shí)間點(diǎn)的定時(shí)發(fā)送,還準(zhǔn)備加上早間新聞資訊以及火車放票信息等內(nèi)容。
以上所述是小編給大家介紹的用python自制微信機(jī)器人,定時(shí)發(fā)送天氣預(yù)報(bào)詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Python中使用Queue和Condition進(jìn)行線程同步的方法
這篇文章主要介紹了Python中使用Queue模塊和Condition對(duì)象進(jìn)行線程同步的方法,配合threading模塊下的線程編程進(jìn)行操作的實(shí)例,需要的朋友可以參考下2016-01-01
python+VTK環(huán)境搭建及第一個(gè)簡(jiǎn)單程序代碼
這篇文章主要介紹了python+VTK環(huán)境搭建及第一個(gè)簡(jiǎn)單程序代碼,簡(jiǎn)單介紹了vtk,然后分享了安裝步驟,最后涉及一個(gè)簡(jiǎn)單的代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12
對(duì)python cv2批量灰度圖片并保存的實(shí)例講解
今天小編就為大家分享一篇對(duì)python cv2批量灰度圖片并保存的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
python對(duì)文件目錄的操作方法實(shí)例總結(jié)
這篇文章主要介紹了python對(duì)文件目錄的操作方法,結(jié)合實(shí)例形式總結(jié)分析了Python針對(duì)文件目錄相關(guān)的遍歷、刪除、移動(dòng)、查找等操作技巧,需要的朋友可以參考下2019-06-06
python實(shí)現(xiàn)bucket排序算法實(shí)例分析
這篇文章主要介紹了python實(shí)現(xiàn)bucket排序算法,實(shí)例分析了Python排序的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-05-05

