大家好我是小阿狸最近刷视频看到很多那种微信公众号推送的,我也去尝试写了一下
今天给大家分享一下教程

首先是打开https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login,打开这个链接去扫码登录,登录之后会给你 
上面这两个,待会需要用

然后滑到下面,找到个二维码,扫码

扫码之后会给右边的微信号,这里这个数据也要用到
找到下面新增模板

标题自己定,对应如图所示,模板具体内容在下面

[XHTML] 纯文本查看 复制代码

?

01

02

03

04

05

06

07

08

09

10

11

12

{{date2.DATA}}

{{demo.DATA}}

{{date.DATA}}

城市:{{city.DATA}}

天气:{{weather.DATA}}

最低气温:{{tempn.DATA}}

最高气温:{{temp.DATA}}

您已经在地球上存活了:{{live.DATA}}天

{{wd.DATA}}

{{english.DATA}}

上面这些配置好之后接下来就要把刚刚的数据都放入python中了
这里需要提前pip install requests这个库

只需要把这八行数据配置好就行了

[Python] 纯文本查看 复制代码

?

1

2

3

4

5

6

7

8

appid``=``'wx065c2sjdhrujerd9'

secret``=``'9490534587fc48c8470fkswie8j2bcdd'

touser``=``[``'onOKZ5vas9nGcnqz1_NWJIjwjeml'``,``'onOKZ5g_yY2e2uZQ45nsAksjeAje'``]``#微信用户id

template_id``=``'gTBAfrI2gDO_vwqQa7VD214kITkP_F-0SkZ-Swijwek'``#模板id

city_id``=``'101191301'``#城市天气id

birthday = "2003-04-10"

info``=``"单身狗也要对自己好一点~"``#底部信息

e``=``'Single people should be kind to themselves'``#底部信息

[Python] 纯文本查看 复制代码

?

001

002

003

004

005

006

007

008

009

010

011

012

013

014

015

016

017

018

019

020

021

022

023

024

025

026

027

028

029

030

031

032

033

034

035

036

037

038

039

040

041

042

043

044

045

046

047

048

049

050

051

052

053

054

055

056

057

058

059

060

061

062

063

064

065

066

067

068

069

070

071

072

073

074

075

076

077

078

079

080

081

082

083

084

085

086

087

088

089

090

091

092

093

094

095

096

097

098

099

100

101

import requests

import json

import time

import datetime

if __name__ =``=``"__main__"``:

    appid``=``'wx065c2sjdhrujerd9'

    secret``=``'9490534587fc48c8470fkswie8j2bcdd'

    touser``=``[``'onOKZ5vas9nGcnqz1_NWJIjwjeml'``,``'onOKZ5g_yY2e2uZQ45nsAksjeAje'``]``#微信用户id

    template_id``=``'gTBAfrI2gDO_vwqQa7VD214kITkP_F-0SkZ-Swijwek'``#模板id

    city_id``=``'101191301'``#城市天气id

    birthday = "2003-04-10"

    info``=``"单身狗也要对自己好一点~"``#底部信息

    e``=``'Single people should be kind to themselves'``#底部信息

    grant_type = 'client_credential'

    url``=``f``'https://api.weixin.qq.com/cgi-bin/token?grant_type={grant_type}&appid={appid}&secret={secret}'

    response``=``requests.get(url).json()

    access_token``=``''

    if response[``'expires_in'``]``=``=``7200``:

        # 获取token

        access_token``=``response[``'access_token'``]

        #天气获取

        headers = {

            'Referer'``: 'http://www.weather.com.cn/'``,

            'User-Agent'``: 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'

        }

        url = f``'http://d1.weather.com.cn/weather_index/{city_id}.html?_=' + str``(``int``(``round``(time.time() * 1000``)))

        #日期计算

        r = requests.get(url, headers``=``headers)

        r.encoding = "utf8"

        res = eval``(r.text.split(``";"``)[``0``].split(``"="``)[``-``1``])

        time``=``res[``'weatherinfo'``][``'fctime'``]

        year``=``time[``0``:``4``]

        month``=``time[``4``:``6``]

        day``=``time[``6``:``8``]

        week_list = [``"星期一"``, "星期二"``, "星期三"``, "星期四"``, "星期五"``, "星期六"``, "星期日"``]

        week``=``week_list[datetime.date(``int``(year),``int``(month),``int``(day)).weekday()]

        date = f``'{year}年{month}月{day}日  {week}'

        date2 = f``'{int(month)}月{day}日'

        # 你活了多久

        birthday_date = datetime.datetime.strptime(birthday, "%Y-%m-%d"``)

        curr_datetime = datetime.datetime.now()

        minus_datetime = curr_datetime - birthday_date

        #发送消息

        sendMessage_url``=``f``"https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={access_token}"

        for user in touser:

            data``=``{

               "touser"``:user,

               "template_id"``:template_id,

               "appid"``:appid,

               "data"``:{

                   "demo"``: {

                       "value"``: "测试语句"``,

                       "color"``: "#AAFF00"

                   },

                   "live"``: {

                       "value"``: minus_datetime.days,

                       "color"``: "#00FF00"

                   },

                   "date2"``: {

                       "value"``: date2,

                       "color"``: "#6B6A66"

                   },

                   "date"``: {

                       "value"``: date,

                       "color"``: "#CAA06A"

                   },

                    "city"``:{

                        "value"``:res[``'weatherinfo'``][``'city'``],

                        "color"``: "#00FF00"

                    },

                   "weather"``:{

                       "value"``:res[``'weatherinfo'``][``'weather'``],

                       "color"``: "#F9AD08"

                   },

                    "tempn"``:{

                        "value"``:res[``'weatherinfo'``][``'tempn'``],

                        "color"``: "#9DB981"

                    },

                   "temp"``:{

                        "value"``:res[``'weatherinfo'``][``'temp'``],

                        "color"``:``"#CAA06A"

                    },

                   "wd"``:{

                        "value"``:info,

                        "color"``:``"#92CAD9"

                    },

                   'english'``:{

                       "value"``:e,

                       "color"``:``"#FF0000"

                   }

               }

           }

            getTemp``=``requests.post(sendMessage_url,data``=``json.dumps(data)).json()

    else``:

        print``(``"appid或secret错误"``)

最后放一张效果图吧

好了就分享到这里了,觉的有用的可以支持一下

放个城市id的文件吧