前段時間找到了一個免費的天氣預報API,費了好段時間把這個API解析並組裝成本身想用的格式了,就想着如何實現天天發送天氣信息給本身。最近無心中發現了wxpy庫,用它來作再合適不過了。如下是wxpy庫的簡介:python
wxpy基於itchat,使用了 Web 微信的通信協議,經過大量接口優化提高了模塊的易用性,並進行豐富的功能擴展。實現了微信登陸、收發消息、搜索好友、數據統計、微信公衆號、微信好友、微信羣基本信息獲取等功能。json
廢話很少說,代碼寫起來。api
操做系統:Windows / Linux服務器
Python版本:3.7.2微信
咱們要實現用Python來發微信,發送的內容是天天最新的天氣信息。很明顯咱們須要完成兩部分的準備,先來看看獲取天氣信息這部份內容。restful
本文咱們用到的第三方庫有requests、wxpyy,若環境尚未,按以下方式進行安裝便可。函數
pip install wxpy pip install requests
這裏我使用的API的請求連接以下:優化
http://t.weather.sojson.com/api/weather/city/city_codeurl
請求方式是GET方法,使用時注意更換爲本身城市對應的city_code,除此以外不用帶任何參數。操作系統
請求是restfull風格,city_code爲9位數字,以下示例:
{ "_id": 58, "id": 59, "pid": 3, "city_code": "101230201", "city_name": "廈門" }
你們能夠從_city.json文件中獲取各個城市對應的編號。該文件我已經放在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並拿到服務器返回的數據 rep = requests.get(url, headers = header) rep.encoding = "utf-8" result = '' weather = rep.tex
這個API接口的返回值內容不少,如下僅展現返回的部分信息。實際使用中僅用到三塊內容,首先是城市信息。
"cityInfo": { "city": "海淀區", //請求城市 "cityId": "101010200", //城市ID "parent": "北京市", //上級,通常是省份 "updateTime": "09:02" //天氣更新時間 }
其次是,該城市當前天氣的空氣相關指數。
"data": { "shidu": "32%", //溼度 "pm25": 35.0, //pm2.5 "pm10": 97.0, //pm10 "quality": "良", //空氣質量 "wendu": "7", //溫度 "ganmao": "極少數敏感人羣應減小戶外活動", //感冒提醒(指數) }
第三部分,該城市當前天氣的溫度風力等另一些指數。
"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文件。
拿到返回值以後,須要解析,並轉換組裝成咱們想要的格式。
# 解析服務器返回的數據,具體可參考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]
這樣咱們的第一步,獲取天氣信息就完成了。接下來就是登陸微信定時發送消息了。
首先要登陸微信,一行代碼就搞定了。這裏其實是掃二維碼登陸了一個Web版的微信。
# 初始化機器人,掃碼登錄微信,適用於Windows系統 bot = Bot() # Linux系統,執行登錄請調用下面的這句 bot = Bot(console_qr=2, cache_path="botoo.pkl")
而後咱們須要定義一個發送消息的函數,將獲取並解析好的天氣信息發送給指定微信好友。
# 調用get_weather函數 GW = get_weather() # 填入你朋友的微信暱稱,注意這裏不是備註,也不是微信賬號 my_friend = bot.friends().search(u'一個暱稱')[0] # 發送微信消息 my_friend.send(u"早上好Y(^o^)Y,這裏是今日份的天氣信息請查收!") my_friend.send(GW) my_friend.send(u"Have a Nice Day!") # 每隔86400秒(1天),發送1次 t = Timer(86400, auto_send) t.start()
接下來,你可使用try...except...語句來實如今消息失敗時發出告警:
try: '''此處爲發送消息的代碼,即上一段內容''' except: # 你的微信暱稱,注意這裏不是備註,也不是微信賬號 my_friend = bot.friends().search('&嫺敲棋子&')[0] my_friend.send(u"報告老闆,今日份的信息發送失敗了!")
最後運行主函數,調用發送消息的函數便可。
# 調用函數進行消息發送 auto_send()
這是我清晨收到的微信消息截圖,看上去還不錯。沒白忙活😉
我把這個腳本丟在了個人樹莓上,掛在後臺一直運行,簡直完美。
這裏僅是實現一個最簡單的定時發送,後續考慮如何實現多個時間點的定時發送,還準備加上早間新聞資訊以及火車放票信息等內容。
關注公衆號「Python專欄」,後臺回覆:zsxq05,獲取本文全套代碼!