html
文的文字及圖片來源於網絡,僅供學習、交流使用,不具備任何商業用途,版權歸原做者全部,若有問題請及時聯繫咱們以做處理。json
做者: PK哥api
PS:若有須要Python學習資料的小夥伴能夠加點擊下方連接自行獲取服務器
http://note.youdao.com/noteshare?id=3054cce4add8a909e784ad934f956cef微信
現在,對於咱們年輕人來講,獲取天氣狀況很方便,可是對於咱們不擅長用手機的父母來講,仍是很吃力,他們用的多的仍是微信吧。爲此,我用不到 40 行代碼寫了一個小工具,天天定時把當天的天氣狀況直接發到微信羣裏。網絡
要獲取天氣狀況,須要一個查詢天氣的接口,網上找了一下,通常都是註冊後送必定調用次數的,我選擇了一個,免費送 500 次查詢次數的。 工具
咱們看看接口的 API 文檔。學習
其中城市名 cityname 和 key 是必填項。網站
http://v.juhe.cn/weather/index?format=2&cityname=%E8%8B%8F%E5%B7%9E&key=您申請的KEYurl
key 值在 juhe.cn 個人接口那一欄中能夠看到。
咱們也能夠事先在 Postman 工具中看看接口可否調通。
Postman 接口工具沒用過的不要緊,他們網頁上也提供了調試工具。
咱們直接用 requests 庫請求接口就能得到 json 格式的天氣信息,json 數據中包含了當天和將來幾天的天氣信息,這裏我只須要當天的,當天數據都在 result 下的 today 裏,提取出來用 return 返回。
咱們經過微信把天氣信息發到羣裏,這裏咱們須要用到調用微信的庫,itchat 庫或者 wxpy 均可以,這裏我用了 wxpy 庫。
先導入 wxpy 庫。
from wxpy import *
咱們把剛纔的請求接口返回的天氣信息整合一下,而後用 wxpy 庫的 search 方法經過羣名稱找到你須要發送天氣消息的這個羣,用 send 方法發送。
我這裏是發送到羣裏,固然,你也能夠直接發送給我的微信。
my_friend = bot.friends().search(u'pk哥')[0]
若是每次都須要咱們手動運行,那就有點麻煩,咱們要讓程序天天在固定時間定時發送,這裏須要用到 Timer 定時器。
Timer 定時器格式:
Timer(86400, get_weather)
第一個參數表示相隔指定時間(單位:秒)後再次調用方法(第二個參數),注意,方法後不要帶括號。
86400 秒就是相隔 24 小時,也就是一天的時間。
t = Timer(86400, get_weather)
t.start()
t.join()
若是信息發送失敗,咱們把信息發給本身,這裏我作了一個異常處理。
1 except BaseException: 2 my_friend = bot.friends().search(u'brucepk')[ 3 0] # 發送不成功,則發送消息給本身,提醒消息發送失敗 4 my_friend.send(u'天氣消息發送失敗')
咱們的程序須要持續運行,那是否是須要一直在電腦上運行啊,這樣有點不現實啊,咱們把它部署到服務器上就能夠搞定了,如今服務器也很便宜,作活動的時候通常 100 元之內就能夠買一年。
在服務器中運行程序,直接掃碼登陸微信,下面是我週五開始運行的,相隔 24 小時後,週六再次調用方法,獲取新的天氣信息。
發到羣裏效果以下圖,固然,你還能夠多加一些接口返回的信息或者本身想說的話
一、個人微信登陸不了網頁版微信 由於 itchat 庫和 wxpy 庫都是調用微信的網頁版接口,若是你的微信註冊比較晚,被限制了網頁版登陸功能,那這個程序你沒法運行。
二、發送不到指定羣 先檢查下羣名稱,把羣名稱一些 emoji 表情符號去掉,這些特殊符號可能致使沒法識別。
羣名稱沒錯的話,看看自動發送信息的這個號有沒有把這個羣添加到通信錄。
這樣,一個定時發送消息的小工具就完成了,你也能夠在上面擴展,加上其餘功能,這樣就更完善了。
jinshan-message.py
1 from twilio.rest import Client 2 import requests 3 from threading import Timer 4 from time import sleep 5 6 # def get_weather(): 7 # url = 'http://v.juhe.cn/weather/index?cityname=上海&key=b0da46b36d3a2cce53fac9cdf51dc98a' # 城市名cityname和key值換成本身的 8 # weather_json = requests.get(url).json() 9 # temperature = weather_json['result']['today']['temperature'] 10 # weather = weather_json['result']['today']['weather'] 11 # week = weather_json['result']['today']['week'] 12 # city = weather_json['result']['today']['city'] 13 # dressing_advice = weather_json['result']['today']['dressing_advice'] 14 # return temperature, weather, week, city, dressing_advice 15 16 def get_msg(): 17 url = 'http://open.iciba.com/dsapi/' # 金山詞霸每日一句 api 連接 18 html = requests.get(url) 19 content = html.json()['content'] # 獲取每日一句英文語句 20 note = html.json()['note'] # 獲取每日一句英文的翻譯語句 21 return content, note 22 23 24 while True: 25 try: 26 content, note = get_msg() 27 msg_all = content + '\n' + note + '\n' + 'from 愛你的人' 28 # Your Account Sid and Auth Token from twilio.com/console 29 # DANGER! This is insecure. See http://twil.io/secure 30 account_sid = '輸入你的account_sid' 31 auth_token = '輸入你的auth_token' 32 client = Client(account_sid, auth_token) 33 message = client.messages \ 34 .create(body=msg_all, from_='+輸入你得到的免費號碼', to='+輸入你驗證的接收號碼') 35 print(message.sid) 36 print(msg_all) 37 38 t = Timer(86400, get_msg) # Timer(定時器)是 Thread 的派生類,用於在指定時間後調用一個方法。 39 t.start() 40 t.join() 41 # 異常處理,發送失敗 42 except: 43 print('發送失敗') 44 break
parent-weather.py
1 from wxpy import * 2 import requests 3 from threading import Timer 4 from time import sleep 5 6 bot = Bot(cache_path=True) # 掃碼登陸微信,若是在Linux環境中運行,加一個參數 bot = Bot(console_qr=-2,cache_path=True) 7 8 9 def get_weather(): 10 url = 'http://v.juhe.cn/weather/index?cityname=上海&key=xxx' # 城市名cityname和key值換成本身的 11 weather_json = requests.get(url).json() 12 temperature = weather_json['result']['today']['temperature'] 13 weather = weather_json['result']['today']['weather'] 14 week = weather_json['result']['today']['week'] 15 city = weather_json['result']['today']['city'] 16 dressing_advice = weather_json['result']['today']['dressing_advice'] 17 return temperature, weather, week, city, dressing_advice 18 19 20 while True: # !!!調試時記得先把while True註釋掉,否則會一直重複發送失敗,一天限制100次調用的,成功後再加上註釋 21 try: 22 temperature, weather, week, city, dressing_advice = get_weather() 23 # 發送到微信羣裏 24 my_groups = bot.groups().search('你的羣名稱')[0] # 換成發送信息的羣名稱 25 msg = '今天是:' + week + '\n' \ 26 + city + '的天氣:' + weather + '\n' \ 27 + '今天溫度:' + temperature +'\n' \ 28 + '穿衣指南:' + dressing_advice 29 print(msg) 30 my_groups.send(msg) 31 32 # 單獨私發微信 33 # my_friend = bot.friends().search(u'pk')[0] # 此處是對方本身的暱稱,不是微信號,也不是你的備註。 34 # my_friend.send(msg) # 發送文字 35 36 t = Timer(86400, get_weather) # Timer(定時器)是 Thread 的派生類,用於在指定時間後調用一個方法。 37 t.start() 38 t.join() 39 # 異常處理,發送失敗,發送提醒消息給本身 40 except BaseException: 41 my_friend = bot.friends().search(u'xxx')[ 42 0] # 發送不成功,則發送消息給本身,提醒消息發送失敗 xxx改爲你本身微信的暱稱 43 my_friend.send(u'天氣消息發送失敗,請中止程序進行調試') 44 break
weather-message.py
1 from twilio.rest import Client 2 import requests 3 from threading import Timer 4 from time import sleep 5 6 def get_weather(): 7 url = 'http://v.juhe.cn/weather/index?cityname=上海&key=輸入你本身的key,在v.juhe.cn網站註冊獲取' # 城市名cityname和key值換成本身的 8 weather_json = requests.get(url).json() 9 temperature = weather_json['result']['today']['temperature'] 10 weather = weather_json['result']['today']['weather'] 11 week = weather_json['result']['today']['week'] 12 city = weather_json['result']['today']['city'] 13 dressing_advice = weather_json['result']['today']['dressing_advice'] 14 return temperature, weather, week, city, dressing_advice 15 16 17 while True: 18 try: 19 temperature, weather, week, city, dressing_advice = get_weather() 20 21 msg = '今天是:' + week + '\n' \ 22 + city + '的天氣:' + weather + '\n' \ 23 + '今天溫度:' + temperature +'\n' \ 24 + '穿衣指南:' + dressing_advice 25 print(msg) 26 27 # Your Account Sid and Auth Token from twilio.com/console 28 # DANGER! This is insecure. See http://twil.io/secure 29 account_sid = '輸入你的account_sid' 30 auth_token = '輸入你的auth_token' 31 client = Client(account_sid, auth_token) 32 33 message = client.messages \ 34 .create(body=msg, from_='輸入你獲取的號碼', to='+輸入你驗證過的號碼') 35 print(message.sid) 36 37 t = Timer(86400, get_weather) # Timer(定時器)是 Thread 的派生類,用於在指定時間後調用一個方法。 38 t.start() 39 t.join() 40 # 異常處理,發送失敗 41 except BaseException: 42 print('發送失敗') 43 break