网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

python设置微信每天发送天气预报(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("\")
                   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/企业微信注册,注册信息如下:

            python设置微信每天发送天气预报(windows环境)

         

  如果进入如下界面,选择如下:

            python设置微信每天发送天气预报(windows环境)

 

  注册完成后登录企业微信管理后台如下:

            python设置微信每天发送天气预报(windows环境)

2 进入企业微信后,先做通信录管理:

   可通过添加别人的方式进行邀请,也可以发送企业微信二维码请别人主动加入,然后再到后台进行分组管理

  特别注意:所有企业微信用户,必须在终端上下载APP,登录进去之后,在内部设置允许微信同步接收消息后,方可在个人微信中获得信息,否则无法在个人微信端获取信息,一旦设置完毕后,企业微信APP可卸载,直接在个人微信中获取信息即可。

             python设置微信每天发送天气预报(windows环境)

3分组完成后,需要从企业微信中分别获取如下信息:

             ①企业ID

             ②自建应用ID

             ③自建应用secret

             ④部门ID

             利用①和③获取access_token

             然后利用获取到的access_token拼接网页,发送数据给②的④群组,或者是②的个人

      具体获取信息截图如下:

      ①获取企业ID

            python设置微信每天发送天气预报(windows环境)

       ②自建应用ID 和③自建应用secret

            python设置微信每天发送天气预报(windows环境) 

           python设置微信每天发送天气预报(windows环境) 

           python设置微信每天发送天气预报(windows环境)

        ④部门ID

            python设置微信每天发送天气预报(windows环境)

或者针对个人用户:

           python设置微信每天发送天气预报(windows环境)

三 完整代码

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("\")
        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 添加定期执行任务

          python设置微信每天发送天气预报(windows环境)

          python设置微信每天发送天气预报(windows环境)

            python设置微信每天发送天气预报(windows环境)

            python设置微信每天发送天气预报(windows环境)

            python设置微信每天发送天气预报(windows环境)

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

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


网页名称:python设置微信每天发送天气预报(windows环境)
网站网址:http://bjjierui.cn/article/giojee.html

其他资讯