IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> python实现微信公众号给你的重要用户推送消息 -> 正文阅读

[Python知识库]python实现微信公众号给你的重要用户推送消息

最近小红书上这个很火,觉得做起来应该也不会太难,捣鼓了两天有了初步成效,还有一些功能没实现(比如定时推送),保姆级教程,大家可以借鉴,动手给自己重要的人做一个,或许可以暖ta一整天- -

1、步骤一

没有python的小白记得先下载python和pycharm,本文基于python实现,浅浅贴个网址https://www.python.orghttps://www.python.org/

PyCharm: the Python IDE for Professional Developers by JetBrainsThe Python & Django IDE with intelligent code completion, on-the-fly error checking, quick-fixes, and much more...https://www.jetbrains.com/pycharm/pycharm下载community免费版即可

2、步骤二

微信公众平台https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login打开并注册一个微信公众号测试号,扫码登录即可

测试账号拥有几乎所有的公众号接口,而个人只能申请订阅号,几乎没有接口可用,并且消息推送还会受到次数限制。如果只是做开发测试的话,那么测试帐号比较好用。

3、步骤三

先给大家看一下成品(只是个模板,具体信息需要大家自行修改)

包含内容有:日期、地理位置、当地位置今日(明日)天气、最高温度、最低温度、风力(大家也可以自行加上湿度等等,并可以根据不同天气提醒对方需要怎么样)、在一起多少天、距离下一个生日多少天、每日一句土味情话(来自🐒 渣男 - 说话的艺术 (lovelive.tools))、每日学一句英语(来自爱词霸),看到别的博主还有什么喝水提醒,大家可以自行发挥~

?以上功能生日和在一起多少天很好实现,我直接贴代码

def get_birthday(birthday, year, today):
    # 获取生日的月和日
    birthday_month = int(birthday.split("-")[1])
    birthday_day = int(birthday.split("-")[2])
    # 今年生日
    year_date = date(year, birthday_month, birthday_day)
    # 计算生日年份,如果还没过,按当年减,如果过了需要+1
    if today > year_date:
        birth_date = date((year + 1), birthday_month, birthday_day)
        birth_day = str(birth_date.__sub__(today)).split(" ")[0]
    elif today == year_date:
        birth_day = 0
    else:
        birth_date = year_date
        birth_day = str(birth_date.__sub__(today)).split(" ")[0]
    return birth_day

?天气、每日一句情话和爱词霸需要通过爬虫获得,在爬之前,将pip更新到最新版本,打开terminal输入pip install --upgrade pip等待更新完成即可

爬的时候要用到beautifulsoup库,同样在terminal输入?pip3 install beautifulsoup4

还要用到request库,?同样在terminal输入pip install requests

我爬取的网站是中国天气网首页 (weather.com.cn)http://www.weather.com.cn/

好处就是url大家只要自行查找一下对应的地区代码替换即可了,天气代码查询可以参照这篇?(41条消息) 中国天气城市代码编号_it_zujun的博客-CSDN博客https://blog.csdn.net/u013634862/article/details/44886769

如北京就是http://www.weather.com.cn/weather/101010100.shtml,直接找到对应的城市代码,将101010100替换即可

我的headers是把所有的复制下来了,所以headers里的referer也记得修改

?修改这两处就可以获得对应地方的天气了,最后记得要把city_name改一下

?代码原理也很简单,访问网站,添加headers是为了模仿用户浏览,通过requests的get获取当前网页的信息,但获得的信息是乱码,所以需要通过’utf-8‘进行解码

利用beautifulsoup提取数据,方式为’lxml‘或者’html.parser‘,我的就只能用后者,前者会报错好像是因为库没下载全--。提取到数据之后,在相关网站按F12,查询自己需要的信息位于哪里,比如日期就存放在’h1‘下面,利用find_all找到所有的日期遍历并存储,取自己需要的即可,比如我的代码里就只需要今明两天的,风力,气温,天气状况都是同理

?代码如下:

def get_weather():
    city_name = '北京'

    headers = {
        'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'Accept-Encoding':'gzip, deflate',
        'Accept-Language':'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'Cache-Control':'max-age=0',
        'Connection':'keep-alive',
        'Cookie':'HttpOnly; userNewsPort0=1; Hm_lvt_080dabacb001ad3dc8b9b9049b36d43b=1660634957; f_city=%E9%87%91%E5%8D%8E%7C101210901%7C; defaultCty=101210901; defaultCtyName=%u91D1%u534E; Hm_lpvt_080dabacb001ad3dc8b9b9049b36d43b=1660639816',
        'Host':'www.weather.com.cn',
        'Referer':'http://www.weather.com.cn/weather1d/101010100.shtml',
        'Upgrade-Insecure-Requests':'1',
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.81 Safari/537.36 Edg/104.0.1293.54'

    }
    url = "http://www.weather.com.cn/weather/101010100.shtml"
    response = get(url = url, headers=headers)

    text = response.content.decode('utf-8')
    soup = bs4.BeautifulSoup(text,'html.parser')
    #存放日期
    list_day = []
    i = 0
    day_list = soup.find_all('h1')
    for each in day_list:
        if i <= 1: #今明两天的数据
            list_day.append(each.text.strip())
            i += 1

    #天气情况
    list_weather = []
    weather_list = soup.find_all('p',class_ = 'wea') #之前报错是因为写了class 改为class_就好了
    for i in weather_list:
        list_weather.append(i.text)
    list_weather = list_weather[0:2] #只取今明两天


    #存放当前温度,和明天的最高温度和最低温度
    tem_list = soup.find_all('p', class_='tem')

    i = 0
    list_tem = []
    for each in tem_list:
        if i >= 0 and i < 2:
            list_tem.append([each.span.text, each.i.text]) #记录明天的最高温度和最低温度
            i += 1

    #风力
    list_wind = []
    wind_list = soup.find_all('p', class_='win')
    for each in wind_list:
        list_wind.append(each.i.text.strip())
    list_wind = list_wind[0:2] #同只取今明两天

    today_date = list_day[0]
    today_weather = list_weather[0]
    today_max = list_tem[0][0] +'℃'
    today_min = list_tem[0][1]
    today_wind = list_wind[0]
    tomorrow = list_day[1]
    tomorrow_weather = list_weather[1]
    tomorrow_max = list_tem[1][0]  +'℃'
    tomorrow_min = list_tem[1][1]
    tomorrow_wind = list_wind[1]


    return city_name,today_date,today_weather,today_max,today_min,today_wind,tomorrow,tomorrow_weather,tomorrow_max,tomorrow_min,tomorrow_wind

在昨天晚上运行的时候发现报错了,原因是因为到了晚上今天的温度只显示一个了(只显示当前温度,不显示最高温度最低温度了),那么在运行的时候就会读取不到两个而报错,浅浅修改了一下代码:如下,主要是更改了存放当前温度那一部分,把i==0的情况单独拎出来,大家可以看下以下两种情况,需要在不同情况下跑不同的代码

?

def get_weather():
    city_name = '北京'

    headers = {
        'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'Accept-Encoding':'gzip, deflate',
        'Accept-Language':'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'Cache-Control':'max-age=0',
        'Connection':'keep-alive',
        'Cookie':'HttpOnly; userNewsPort0=1; Hm_lvt_080dabacb001ad3dc8b9b9049b36d43b=1660634957; f_city=%E9%87%91%E5%8D%8E%7C101210901%7C; defaultCty=101210901; defaultCtyName=%u91D1%u534E; Hm_lpvt_080dabacb001ad3dc8b9b9049b36d43b=1660639816',
        'Host':'www.weather.com.cn',
        'Referer':'http://www.weather.com.cn/weather1d/101010100.shtml',
        'Upgrade-Insecure-Requests':'1',
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.81 Safari/537.36 Edg/104.0.1293.54'

    }
    url = "http://www.weather.com.cn/weather/101010100.shtml"
    response = get(url = url, headers=headers)

    text = response.content.decode('utf-8')
    soup = bs4.BeautifulSoup(text,'html.parser')
    #存放日期
    list_day = []
    i = 0
    day_list = soup.find_all('h1')
    for each in day_list:
        if i <= 1: #今明两天的数据
            list_day.append(each.text.strip())
            i += 1

    #天气情况
    list_weather = []
    weather_list = soup.find_all('p',class_ = 'wea') #之前报错是因为写了class 改为class_就好了
    for i in weather_list:
        list_weather.append(i.text)
    list_weather = list_weather[0:2] #只取今明两天


    #存放当前温度,和明天的最高温度和最低温度
    tem_list = soup.find_all('p', class_='tem')

    i = 0
    list_tem = []
    for each in tem_list:
        if i == 0:
            list_tem.append(each.i.text)
            i += 1
        elif i > 0 and i < 2:
            list_tem.append([each.span.text, each.i.text])
            i += 1

    #风力
    list_wind = []
    wind_list = soup.find_all('p', class_='win')
    for each in wind_list:
        list_wind.append(each.i.text.strip())
    list_wind = list_wind[0:2] #同只取今明两天

    today_date = list_day[0]
    today_weather = list_weather[0]
    now = list_tem[0]
    today_wind = list_wind[0]
    tomorrow = list_day[1]
    tomorrow_weather = list_weather[1]
    tomorrow_max = list_tem[1][0]
    tomorrow_min = list_tem[1][1]
    tomorrow_wind = list_wind[1]


    return city_name,today_date,today_weather,now,today_wind,tomorrow,tomorrow_weather,tomorrow_max,tomorrow_min,tomorrow_wind

?天气讲完了,爱词霸和每日一句更好实现了,均利用json实现,记得import json,爱词霸这个是每天更新一次,所以一天多次输出都是一样的,但是每日情话这个是每次输出都不一样,里面还有些舔狗语录笑死了,这两个网站信息很单一,所以我都没加headers --。大家要写也可自己加上

def get_daily_love():
    #每日一句情话
    url = "https://api.lovelive.tools/api/SweetNothings/Serialization/Json"
    r = requests.get(url)
    all_dict = json.loads(r.text)
    sentence = all_dict['returnObj'][0]
    daily_love = sentence
    return daily_love
def get_ciba():
    #每日一句英语(来自爱词霸)
    url = "http://open.iciba.com/dsapi/"
    r = get(url)
    note_en = r.json()["content"]
    note_ch = r.json()["note"]
    return note_ch, note_en

?接下来要设置字体颜色,如果要随机可以参考以下代码

def get_color():
    # 获取随机颜色
    get_colors = lambda n: list(map(lambda i: "#" + "%06x" % random.randint(0, 0xFFFFFF), range(n)))
    color_list = get_colors(100)
    return random.choice(color_list)

但是太花了并不会太好看,尤其是元素过多时,因此我修改了一下就变成了

颜色代码可参考此文章(41条消息) Python 颜色代码大全_奋斗中的打工人的博客-CSDN博客_python颜色代码表https://blog.csdn.net/summerriver1/article/details/125215461?spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-2-125215461-blog-87262631.t0_layer_eslanding_s&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-2-125215461-blog-87262631.t0_layer_eslanding_s&utm_relevant_index=5

def get_color():
    # 往list中填喜欢的颜色即可
    color_list = ['#6495ED','#3CB371']

    return random.choice(color_list)

在设置data的时候将元素和颜色传入即可,比如你的city值为city_name(北京),颜色为你填充的颜色里的随机一个,也可以自行设置哪个值为什么颜色

?最后,解决access_token,也是利用get来获取,此处config为一个txt文件,方便后续修改

def get_access_token():
    # appId
    app_id = config["app_id"]
    # appSecret
    app_secret = config["app_secret"]
    post_url = ("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}"
                .format(app_id, app_secret))
    try:
        access_token = get(post_url).json()['access_token']
    except KeyError:
        print("获取access_token失败,请检查app_id和app_secret是否正确")
        os.system("pause")
        sys.exit(1)
    # print(access_token)
    return access_token

重要步骤讲完了 贴一下两个温度版代码:

import random
from time import time, localtime
from requests import get, post
from datetime import datetime, date
import sys
import os
import bs4
import requests
import json


def get_color():
    # 往list中填喜欢的颜色即可
    color_list = ['#6495ED','#3CB371']

    return random.choice(color_list)


def get_access_token():
    # appId
    app_id = config["app_id"]
    # appSecret
    app_secret = config["app_secret"]
    post_url = ("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}"
                .format(app_id, app_secret))
    try:
        access_token = get(post_url).json()['access_token']
    except KeyError:
        print("获取access_token失败,请检查app_id和app_secret是否正确")
        os.system("pause")
        sys.exit(1)
    # print(access_token)
    return access_token


def get_weather():
    city_name = '北京'

    headers = {
        'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'Accept-Encoding':'gzip, deflate',
        'Accept-Language':'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'Cache-Control':'max-age=0',
        'Connection':'keep-alive',
        'Cookie':'HttpOnly; userNewsPort0=1; Hm_lvt_080dabacb001ad3dc8b9b9049b36d43b=1660634957; f_city=%E9%87%91%E5%8D%8E%7C101210901%7C; defaultCty=101210901; defaultCtyName=%u91D1%u534E; Hm_lpvt_080dabacb001ad3dc8b9b9049b36d43b=1660639816',
        'Host':'www.weather.com.cn',
        'Referer':'http://www.weather.com.cn/weather1d/101010100.shtml',
        'Upgrade-Insecure-Requests':'1',
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.81 Safari/537.36 Edg/104.0.1293.54'

    }
    url = "http://www.weather.com.cn/weather/101010100.shtml"
    response = get(url = url, headers=headers)

    text = response.content.decode('utf-8')
    soup = bs4.BeautifulSoup(text,'html.parser')
    #存放日期
    list_day = []
    i = 0
    day_list = soup.find_all('h1')
    for each in day_list:
        if i <= 1: #今明两天的数据
            list_day.append(each.text.strip())
            i += 1

    #天气情况
    list_weather = []
    weather_list = soup.find_all('p',class_ = 'wea') #之前报错是因为写了class 改为class_就好了
    for i in weather_list:
        list_weather.append(i.text)
    list_weather = list_weather[0:2] #只取今明两天


    #存放当前温度,和明天的最高温度和最低温度
    tem_list = soup.find_all('p', class_='tem')

    i = 0
    list_tem = []
    for each in tem_list:
        if i >= 0 and i < 2:
            list_tem.append([each.span.text, each.i.text]) #记录明天的最高温度和最低温度
            i += 1

    #风力
    list_wind = []
    wind_list = soup.find_all('p', class_='win')
    for each in wind_list:
        list_wind.append(each.i.text.strip())
    list_wind = list_wind[0:2] #同只取今明两天

    today_date = list_day[0]
    today_weather = list_weather[0]
    today_max = list_tem[0][0] +'℃'
    today_min = list_tem[0][1]
    today_wind = list_wind[0]
    tomorrow = list_day[1]
    tomorrow_weather = list_weather[1]
    tomorrow_max = list_tem[1][0]  +'℃'
    tomorrow_min = list_tem[1][1]
    tomorrow_wind = list_wind[1]


    return city_name,today_date,today_weather,today_max,today_min,today_wind,tomorrow,tomorrow_weather,tomorrow_max,tomorrow_min,tomorrow_wind



def get_birthday(birthday, year, today):
    # 获取生日的月和日
    birthday_month = int(birthday.split("-")[1])
    birthday_day = int(birthday.split("-")[2])
    # 今年生日
    year_date = date(year, birthday_month, birthday_day)
    # 计算生日年份,如果还没过,按当年减,如果过了需要+1
    if today > year_date:
        birth_date = date((year + 1), birthday_month, birthday_day)
        birth_day = str(birth_date.__sub__(today)).split(" ")[0]
    elif today == year_date:
        birth_day = 0
    else:
        birth_date = year_date
        birth_day = str(birth_date.__sub__(today)).split(" ")[0]
    return birth_day

def get_daily_love():
    #每日一句情话
    url = "https://api.lovelive.tools/api/SweetNothings/Serialization/Json"
    r = requests.get(url)
    all_dict = json.loads(r.text)
    sentence = all_dict['returnObj'][0]
    daily_love = sentence
    return daily_love

def get_ciba():
    #每日一句英语(来自爱词霸)
    url = "http://open.iciba.com/dsapi/"

    r = get(url)
    note_en = r.json()["content"]
    note_ch = r.json()["note"]
    return note_ch, note_en



def send_message(to_user, access_token, city_name,today_date,today_weather,today_max,today_min,today_wind,tomorrow,tomorrow_weather,tomorrow_max,tomorrow_min,tomorrow_wind,daily_love,note_ch, note_en):
    url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={}".format(access_token)
    week_list = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
    year = localtime().tm_year
    month = localtime().tm_mon
    day = localtime().tm_mday
    today = datetime.date(datetime(year=year, month=month, day=day))
    week = week_list[today.isoweekday() % 7]
    # 获取在一起的日子的日期格式
    love_year = int(config["love_date"].split("-")[0])
    love_month = int(config["love_date"].split("-")[1])
    love_day = int(config["love_date"].split("-")[2])
    love_date = date(love_year, love_month, love_day)
    # 获取在一起的日期差
    love_days = str(today.__sub__(love_date)).split(" ")[0]
    # 获取所有生日数据
    birthdays = {}
    for k, v in config.items():
        if k[0:5] == "birth":
            birthdays[k] = v
    data = {
        "touser": to_user,
        "template_id": config["template_id"],
        "url": "http://weixin.qq.com/download",
        "topcolor": "#FF0000",
        "data": {
            "date": {
                "value": "{} {}".format(today, week),
                "color": get_color()
            },
            "city": {
                "value": city_name,
                "color": get_color()
            },
            "today": {
                "value": today_date,
                "color": get_color()
            },
            "today_weather": {
                "value": today_weather,
                "color": get_color()
            },
            "today_max": {
                "value": today_max,
                "color": get_color()
            },
            "today_min": {
                "value": today_min,
                "color": get_color()
            },
            "today_wind": {
                "value": today_wind,
                "color": get_color()
            },
            "tomorrow": {
                "value": tomorrow,
                "color": get_color()
            },
            "tomorrow_weather": {
                "value": tomorrow_weather,
                "color": get_color()
            },
            "tomorrow_max": {
                "value": tomorrow_max,
                "color": get_color()
            },
            "tomorrow_min": {
                "value": tomorrow_min,
                "color": get_color()
            },
            "tomorrow_wind": {
                "value": tomorrow_wind,
                "color": get_color()
            },
            "daily_love": {
                "value": daily_love,
                "color": get_color()
            },
            "birthday": {
                "value": birthdays,
                "color": get_color()
            },
            "note_en": {
                "value": note_en,
                "color": get_color()
            },
            "note_ch": {
                "value": note_ch,
                "color": get_color()
            },
            "love_day": {
                "value": love_days,
                "color": get_color()
            }
        }
    }
    for key, value in birthdays.items():
        # 获取距离下次生日的时间
        birth_day = get_birthday(value, year, today)
        # 将生日数据插入data
        data["data"][key] = {"value": birth_day, "color": get_color()}
    headers = {
        'Content-Type': 'application/json',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                      'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
    }
    response = post(url, headers=headers, json=data).json()
    if response["errcode"] == 40037:
        print("推送消息失败,请检查模板id是否正确")
    elif response["errcode"] == 40036:
        print("推送消息失败,请检查模板id是否为空")
    elif response["errcode"] == 40003:
        print("推送消息失败,请检查微信号是否正确")
    elif response["errcode"] == 0:
        print("推送消息成功")
    else:
        print(response)


if __name__ == "__main__":
    try:
        with open("config.txt", encoding="utf-8") as f:
            config = eval(f.read())
    except FileNotFoundError:
        print("推送消息失败,请检查config.txt文件是否与程序位于同一路径")
        os.system("pause")
        sys.exit(1)
    except SyntaxError:
        print("推送消息失败,请检查配置文件格式是否正确")
        os.system("pause")
        sys.exit(1)

    # 获取accessToken
    accessToken = get_access_token()
    # 接收的用户
    users = config["user"]
    # 传入省份和市获取天气信息
    city_name,today_date,today_weather,today_max,today_min,today_wind,tomorrow,tomorrow_weather,tomorrow_max,tomorrow_min,tomorrow_wind = get_weather()
    # 获取每日情话
    daily_love = get_daily_love()
    #获取每日一句英语
    note_ch, note_en = get_ciba()
    # 公众号推送消息
    for user in users:
        send_message(user, accessToken, city_name,today_date,today_weather,today_max,today_min,today_wind,tomorrow,tomorrow_weather,tomorrow_max,tomorrow_min,tomorrow_wind,daily_love,note_ch, note_en)
    os.system("pause")

?一个温度版代码:

import random
from time import time, localtime
from requests import get, post
from datetime import datetime, date
import sys
import os
import bs4
import requests
import json


def get_color():
    # 往list中填喜欢的颜色即可
    color_list = ['#6495ED','#3CB371']

    return random.choice(color_list)


def get_access_token():
    # appId
    app_id = config["app_id"]
    # appSecret
    app_secret = config["app_secret"]
    post_url = ("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}"
                .format(app_id, app_secret))
    try:
        access_token = get(post_url).json()['access_token']
    except KeyError:
        print("获取access_token失败,请检查app_id和app_secret是否正确")
        os.system("pause")
        sys.exit(1)
    # print(access_token)
    return access_token


def get_weather():
    city_name = '北京'

    headers = {
        'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'Accept-Encoding':'gzip, deflate',
        'Accept-Language':'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'Cache-Control':'max-age=0',
        'Connection':'keep-alive',
        'Cookie':'HttpOnly; userNewsPort0=1; Hm_lvt_080dabacb001ad3dc8b9b9049b36d43b=1660634957; f_city=%E9%87%91%E5%8D%8E%7C101210901%7C; defaultCty=101210901; defaultCtyName=%u91D1%u534E; Hm_lpvt_080dabacb001ad3dc8b9b9049b36d43b=1660639816',
        'Host':'www.weather.com.cn',
        'Referer':'http://www.weather.com.cn/weather1d/101010100.shtml',
        'Upgrade-Insecure-Requests':'1',
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.81 Safari/537.36 Edg/104.0.1293.54'

    }
    url = "http://www.weather.com.cn/weather/101010100.shtml"
    response = get(url = url, headers=headers)

    text = response.content.decode('utf-8')
    soup = bs4.BeautifulSoup(text,'html.parser')
    #存放日期
    list_day = []
    i = 0
    day_list = soup.find_all('h1')
    for each in day_list:
        if i <= 1: #今明两天的数据
            list_day.append(each.text.strip())
            i += 1

    #天气情况
    list_weather = []
    weather_list = soup.find_all('p',class_ = 'wea') #之前报错是因为写了class 改为class_就好了
    for i in weather_list:
        list_weather.append(i.text)
    list_weather = list_weather[0:2] #只取今明两天


    #存放当前温度,和明天的最高温度和最低温度
    tem_list = soup.find_all('p', class_='tem')

    i = 0
    list_tem = []
    for each in tem_list:
        if i == 0:
            list_tem.append(each.i.text)
            i += 1
        elif i > 0 and i < 2:
            list_tem.append([each.span.text, each.i.text])
            i += 1

    #风力
    list_wind = []
    wind_list = soup.find_all('p', class_='win')
    for each in wind_list:
        list_wind.append(each.i.text.strip())
    list_wind = list_wind[0:2] #同只取今明两天

    today_date = list_day[0]
    today_weather = list_weather[0]
    now = list_tem[0]
    today_wind = list_wind[0]
    tomorrow = list_day[1]
    tomorrow_weather = list_weather[1]
    tomorrow_max = list_tem[1][0]
    tomorrow_min = list_tem[1][1]
    tomorrow_wind = list_wind[1]


    return city_name,today_date,today_weather,now,today_wind,tomorrow,tomorrow_weather,tomorrow_max,tomorrow_min,tomorrow_wind



def get_birthday(birthday, year, today):
    # 获取生日的月和日
    birthday_month = int(birthday.split("-")[1])
    birthday_day = int(birthday.split("-")[2])
    # 今年生日
    year_date = date(year, birthday_month, birthday_day)
    # 计算生日年份,如果还没过,按当年减,如果过了需要+1
    if today > year_date:
        birth_date = date((year + 1), birthday_month, birthday_day)
        birth_day = str(birth_date.__sub__(today)).split(" ")[0]
    elif today == year_date:
        birth_day = 0
    else:
        birth_date = year_date
        birth_day = str(birth_date.__sub__(today)).split(" ")[0]
    return birth_day

def get_daily_love():
    #每日一句情话
    url = "https://api.lovelive.tools/api/SweetNothings/Serialization/Json"
    r = requests.get(url)
    all_dict = json.loads(r.text)
    sentence = all_dict['returnObj'][0]
    daily_love = sentence
    return daily_love

def get_ciba():
    #每日一句英语(来自爱词霸)
    url = "http://open.iciba.com/dsapi/"
    headers = {
        'Content-Type': 'application/json',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                      'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
    }
    r = get(url, headers=headers)
    note_en = r.json()["content"]
    note_ch = r.json()["note"]
    return note_ch, note_en



def send_message(to_user, access_token, city_name,today_date,today_weather,now,today_wind,tomorrow,tomorrow_weather,tomorrow_max,tomorrow_min,tomorrow_wind,daily_love,note_ch, note_en):
    url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={}".format(access_token)
    week_list = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
    year = localtime().tm_year
    month = localtime().tm_mon
    day = localtime().tm_mday
    today = datetime.date(datetime(year=year, month=month, day=day))
    week = week_list[today.isoweekday() % 7]
    # 获取在一起的日子的日期格式
    love_year = int(config["love_date"].split("-")[0])
    love_month = int(config["love_date"].split("-")[1])
    love_day = int(config["love_date"].split("-")[2])
    love_date = date(love_year, love_month, love_day)
    # 获取在一起的日期差
    love_days = str(today.__sub__(love_date)).split(" ")[0]
    # 获取所有生日数据
    birthdays = {}
    for k, v in config.items():
        if k[0:5] == "birth":
            birthdays[k] = v
    data = {
        "touser": to_user,
        "template_id": config["template_id"],
        "url": "http://weixin.qq.com/download",
        "topcolor": "#FF0000",
        "data": {
            "date": {
                "value": "{} {}".format(today, week),
                "color": get_color()
            },
            "city": {
                "value": city_name,
                "color": get_color()
            },
            "today": {
                "value": today_date,
                "color": get_color()
            },
            "today_weather": {
                "value": today_weather,
                "color": get_color()
            },
            "now": {
                "value": now,
                "color": get_color()
            },
            "today_wind": {
                "value": today_wind,
                "color": get_color()
            },
            "tomorrow": {
                "value": tomorrow,
                "color": get_color()
            },
            "tomorrow_weather": {
                "value": tomorrow_weather,
                "color": get_color()
            },
            "tomorrow_max": {
                "value": tomorrow_max,
                "color": get_color()
            },
            "tomorrow_min": {
                "value": tomorrow_min,
                "color": get_color()
            },
            "tomorrow_wind": {
                "value": tomorrow_wind,
                "color": get_color()
            },
            "daily_love": {
                "value": daily_love,
                "color": get_color()
            },
            "birthday": {
                "value": birthdays,
                "color": get_color()
            },
            "note_en": {
                "value": note_en,
                "color": get_color()
            },
            "note_ch": {
                "value": note_ch,
                "color": get_color()
            },
            "love_day": {
                "value": love_days,
                "color": get_color()
            }
        }
    }
    for key, value in birthdays.items():
        # 获取距离下次生日的时间
        birth_day = get_birthday(value, year, today)
        # 将生日数据插入data
        data["data"][key] = {"value": birth_day, "color": get_color()}
    headers = {
        'Content-Type': 'application/json',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                      'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
    }
    response = post(url, headers=headers, json=data).json()
    if response["errcode"] == 40037:
        print("推送消息失败,请检查模板id是否正确")
    elif response["errcode"] == 40036:
        print("推送消息失败,请检查模板id是否为空")
    elif response["errcode"] == 40003:
        print("推送消息失败,请检查微信号是否正确")
    elif response["errcode"] == 0:
        print("推送消息成功")
    else:
        print(response)


if __name__ == "__main__":
    try:
        with open("config.txt", encoding="utf-8") as f:
            config = eval(f.read())
    except FileNotFoundError:
        print("推送消息失败,请检查config.txt文件是否与程序位于同一路径")
        os.system("pause")
        sys.exit(1)
    except SyntaxError:
        print("推送消息失败,请检查配置文件格式是否正确")
        os.system("pause")
        sys.exit(1)

    # 获取accessToken
    accessToken = get_access_token()
    # 接收的用户
    users = config["user"]
    # 传入省份和市获取天气信息
    city_name,today_date,today_weather,now,today_wind,tomorrow,tomorrow_weather,tomorrow_max,tomorrow_min,tomorrow_wind = get_weather()
    # 获取每日情话
    daily_love = get_daily_love()
    #获取每日一句英语
    note_ch, note_en = get_ciba()
    # 公众号推送消息
    for user in users:
        send_message(user, accessToken, city_name,today_date,today_weather,now,today_wind,tomorrow,tomorrow_weather,tomorrow_max,tomorrow_min,tomorrow_wind,daily_love,note_ch, note_en)
    os.system("pause")

4、步骤四

代码部分讲完了,需要来讲一下公众号测试号的设置

两个温度版:

参考以上模板,{{xx.DATA}}就是你代码里return出来的各个值,把其填到相应位置即可,

纯文字方便复制版:

{{date.DATA}}?

宝贝现在位于:{{city.DATA}}

{{today.DATA}}天气{{today_weather.DATA}},今天的最高温度为{{today_max.DATA}},最低温度为{{today_min.DATA}},风力为{{today_wind.DATA}}

顺便给宝贝播报一下{{tomorrow.DATA}}的天气:{{tomorrow_weather.DATA}},最高温度{{tomorrow_max.DATA}},最低温度{{tomorrow_min.DATA}},风力{{tomorrow_wind.DATA}}

今天是我们在一起的第{{love_day.DATA}}天

距离宝贝的生日还有{{birthday1.DATA}}天

{{daily_love.DATA}}

{{note_ch.DATA}}{{note_en.DATA}}?

?文字+注释版:

{{date.DATA}}? #日期+星期几

宝贝现在位于:{{city.DATA}} #城市名字

{{today.DATA}}?天气{{today_weather.DATA}},今天的最高温度为{{today_max.DATA}},最低温度为{{today_min.DATA}},风力为{{today_wind.DATA}} #(今天)天气(怎么样),今天的最高温度为(多少),最低温度为(多少),风力为(多少),括号的值均为代码里的对应值

顺便给宝贝播报一下{{tomorrow.DATA}}的天气:{{tomorrow_weather.DATA}},最高温度{{tomorrow_max.DATA}},最低温度{{tomorrow_min.DATA}},风力{{tomorrow_wind.DATA}} #同上

今天是我们在一起的第{{love_day.DATA}}天 #恋爱天数

距离宝贝的生日还有{{birthday1.DATA}}天 #离生日多少天那个值

{{daily_love.DATA}} #每日一句情话

{{note_ch.DATA}}{{note_en.DATA}}? #每日一句英语

一个温度版:

{{date.DATA}}

宝贝现在位于:{{city.DATA}}

{{today.DATA}}天气{{today_weather.DATA}},当前温度为{{now.DATA}},风力为{{today_wind.DATA}}

顺便给宝贝播报一下{{tomorrow.DATA}}的天气:{{tomorrow_weather.DATA}},最高温度{{tomorrow_max.DATA}},最低温度{{tomorrow_min.DATA}},风力{{tomorrow_wind.DATA}}

今天是我们在一起的第{{love_day.DATA}}天

距离宝贝的生日还有{{birthday1.DATA}}天

{{daily_love.DATA}}

{{note_ch.DATA}}{{note_en.DATA}}

?5、步骤五

让对方扫码关注你的测试号,(先自己关注测试一下能不能跑出来吧)

把测试号的appid,app_secret,接受微信号,模板消息,生日、恋爱日子替换即可,注意生日是公历,农历需要自己换算为公历

?GitHub打开好慢,我懒得贴txt了,需要的可以私我,或者自己改一下

全部弄完之后 润一下应该就会播报了

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2022-08-19 19:00:11  更:2022-08-19 19:00:30 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年12日历 -2024/12/26 15:05:17-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码
数据统计