总况之整体思路:

         à从网络上实时获取天气信息,“为字符串格式”

         à注册企业微信

         à写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 requestsimport 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_next5daysif __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 添加定期执行任务

          

          

            

            

            

         剩下就点击确定就完成了,以后每天都能够收到需要的天气预报了。完毕!

   引申:如果还有什么需要定时发送的消息,可透过此方式操作....