零基礎設置微信每天發送天氣預報(windows環境)

總況之整體思路:

         à從網絡上實時獲取天氣信息,爲字符串格式

         à註冊企業微信

         à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/ 企業微信註冊,註冊信息如下:

            image.png

         

  如果進入如下界面,選擇如下:

            image.png

 

  註冊完成後登錄企業微信管理後臺如下:

            image.png

2 進入企業微信後,先做通信錄管理:

   可通過添加別人的方式進行邀請,也可以發送企業微信二維碼請別人主動加入,然後再到後臺進行分組管理

  特別注意:所有企業微信用戶,必須在終端上下載APP,登錄進去之後,在內部設置允許微信同步接收消息後,方可在個人微信中獲得信息,否則無法在個人微信端獲取信息,一旦設置完畢後,企業微信APP可卸載,直接在個人微信中獲取信息即可。

             image.png

3分組完成後,需要從企業微信中分別獲取如下信息:

             ①企業ID

             ②自建應用ID

             ③自建應用secret

             ④部門ID

             利用獲取access_token

             然後利用獲取到的access_token拼接網頁,發送數據給羣組,或者是的個人

      具體獲取信息截圖如下:

      ①獲取企業ID

            image.png

       ②自建應用ID 自建應用secret

            image.png 

           image.png 

           image.png

        ④部門ID

            image.png

或者針對個人用戶

           image.png

完整代碼

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窗口安裝pyinstallerpip install pyintaller,然後執行pyinstaller命令

         例如:pyinstaller –F c:/weather.py

         打包完成後,會生成一個weather.exe的可執行文件

2 添加定期執行任務

          image.png

          image.png

            image.png

            image.png

            image.png

         剩下就點擊確定就完成了,以後每天都能夠收到需要的天氣預報了。完畢!

   引申:如果還有什麼需要定時發送的消息,可透過此方式操作....


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章