總況之整體思路:
???????? à從網絡上實時獲取天氣信息,“為字符串格式”
???????? à注冊企業微信
???????? à寫python代碼,利用企業微信群發推送消息的功能,將獲取的天氣信息發送給希望發送的人
???????? à制作定時任務(利用windows環境 計劃任務實現)
一 獲取天氣信息:
???????? 定義一個函數,獲取信息后做簡單處理,返回字符串
???????? 代碼如下:
def?get_weather_next5days(): ?????????import?json,re ?????????import?requests ?????????city_code="101180106"?#此處以鄭州為例,如需要其他城市編碼,可下載http://api.help.bj.cn/api/CityCode.XLS ?????????api_url="http://api.help.bj.cn/apis/weather6d/?id="+city_code ?????????res=requests.get(api_url) ?????????result=json.loads(res.text) ?????????str1="" ?????????for?i?in?result["data"]["forecast"]: ???????????????????date=i["date"] ???????????????????weather=i["weather"] ???????????????????tempe=i["templow"]+"~"+i["temphigh"] ???????????????????windforce=i["windforce"] ???????????????????r=re.compile("\<!\[CDATA\[(.*?)\]\]\>") ???????????????????a=re.findall(r,windforce) ???????????????????wind=i["wind"]+a[0] ???????????????????str=date+'?'+weather+"?"+tempe+"度?"+wind+"\n" ???????????????????str1+=str ?????????weather_next5days="鄭州未來5天天氣\n"+str1 ?????????return?weather_next5days
? ? ? ??
二 企業微信注冊及相關說明
1 企業微信注冊
? 登錄微信主頁:https://work.weixin.qq.com/ 企業微信注冊,注冊信息如下:
????????????
? ? ? ? ?
? 如果進入如下界面,選擇如下:
????????????
?
? 注冊完成后登錄企業微信管理后臺如下:
? ? ? ? ? ??
2 進入企業微信后,先做通信錄管理:
? ?可通過添加別人的方式進行邀請,也可以發送企業微信二維碼請別人主動加入,然后再到后臺進行分組管理
??特別注意:所有企業微信用戶,必須在終端上下載APP,登錄進去之后,在內部設置允許微信同步接收消息后,方可在個人微信中獲得信息,否則無法在個人微信端獲取信息,一旦設置完畢后,企業微信APP可卸載,直接在個人微信中獲取信息即可。
? ? ? ? ? ? ?
3分組完成后,需要從企業微信中分別獲取如下信息:
???????????? ①企業ID
???????? ????②自建應用ID
???????? ????③自建應用secret
???????? ????④部門ID
???????????? 利用①和③獲取access_token
???????? ????然后利用獲取到的access_token拼接網頁,發送數據給②的④群組,或者是②的個人
? ????具體獲取信息截圖如下:
? ? ? ①獲取企業ID
????????????
? ? ? ?②自建應用ID 和③自建應用secret
?????????????
?? ? ? ? ???
?? ? ? ? ??
????????④部門ID
????????????
或者針對個人用戶:
????????? ?
三 完整代碼
import?requests import?json,re ? class?WeChat(): ????def?__init__(self): ????????self.CORPID?=?'企業ID?'??#企業ID,在管理后臺獲取 ????????self.CORPSECRET?=?'自建應用的Secret?'#自建應用的Secret,每個自建應用里都有單獨的secret ????????self.AGENTID?=?'1000005'??#應用ID,在后臺應用中獲取 ????????self.TOPARTY?=?"6"??#?接收者部門ID ? ????def?_get_access_token(self): ????????url?=?'https://qyapi.weixin.qq.com/cgi-bin/gettoken' ????????values?=?{'corpid':?self.CORPID, ??????????????????'corpsecret':?self.CORPSECRET, ??????????????????} ????????req?=?requests.post(url,?params=values) ????????data?=?json.loads(req.text) ????????return?data["access_token"] ? ????def?send_data(self,?message): ????????send_url?=?'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token='?+?self._get_access_token() ????????send_values?=?{ ????????????"toparty"?:?"6",?#發送信息給群組,6表示部門ID ????????????#?"touser":?self.TOUSER,#發送信息給個人,對象不是微信號,也不是微信昵稱,是企業微信通訊錄中的個人賬號 ????????????"msgtype":?"text",?#如果格式為text,收到的信息類似于企業給個人發信息的方式,無標題,企業跟個人同等角色,類似于跟企業個人聊天, ????????????"agentid":?self.AGENTID, ????????????"text":?{ ????????????????"content":?message?#如果格式為text,內容只需要增加content即可 ????????????????}, ????????????"safe":?"0" ????????????} ????????send_msges=(bytes(json.dumps(send_values),?'utf-8')) ????????respone?=?requests.post(send_url,?send_msges) ????????respone?=?respone.json()???#當返回的數據是json串的時候直接用.json即可將respone轉換成字典 ????????print(respone) ????????return?respone["errmsg"] def?get_weather_next5days(): ????city_code="101180106"?#此處以鄭州為例,如需要其他城市編碼,可下載http://api.help.bj.cn/api/CityCode.XLS ????api_url="http://api.help.bj.cn/apis/weather6d/?id="+city_code ????res=requests.get(api_url) ????result=json.loads(res.text) ????str1="" ????for?i?in?result["data"]["forecast"]: ????????date=i["date"] ????????weather=i["weather"] ????????tempe=i["templow"]+"~"+i["temphigh"] ????????windforce=i["windforce"] ????????r=re.compile("\<!\[CDATA\[(.*?)\]\]\>") ????????a=re.findall(r,windforce) ????????wind=i["wind"]+a[0] ????????str=date+'?'+weather+"?"+tempe+"度?"+wind+"\n" ????????str1+=str ????weather_next5days="鄭州未來5天天氣\n"+str1 ????return?weather_next5days if?__name__?==?'__main__': ????result=get_weather_next5days()#獲取天氣信息 ????wx?=?WeChat() ????send_res=wx.send_data(result)
四 制作exe執行文件,然后添加到windows任務計劃中,每天定時獲取并發送
1 python程序打包:
???????? 將第三步制作的.py文件,通過pyinstaller –F 文件路徑 進行封裝打包
???????? Ps:先在cmd窗口安裝pyinstaller:pip install pyintaller,然后執行pyinstaller命令
???????? 例如:pyinstaller –F c:/weather.py
???????? 打包完成后,會生成一個weather.exe的可執行文件
2 添加定期執行任務
???????? ?
? ? ? ? ??
? ? ? ? ? ??
????????????
????????????
?????????剩下就點擊確定就完成了,以后每天都能夠收到需要的天氣預報了。完畢!
? ?引申:如果還有什么需要定時發送的消息,可透過此方式操作....
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。