一文教你利用Python制作一个生日提醒
作者:星安果 时间:2022-12-12 09:20:43
在国内,大部分人都是过农历生日,然后借助日历工具获取农历日期对应的阳历日期,以这一天来过生!
这里还有一个痛点,即:每一年的农历生日对应的阳历日期都不一样
本篇文章将教你利用 Python 制作一个简单的生日提醒
1. 实战
具体操作步骤如下
1-1 安装依赖
# 安装依赖
pip3 install zhdate
pip3 install pymysql
其中,zhdate 模块用于中国农历、阳历之间的转换,并且支持日期差额计算
项目地址:
https://github.com/CutePandaSh/zhdate
1-2 创建数据表
创建一条数据表
create table birthday
(
id int auto_increment
primary key,
name varchar(100) not null comment '名称',
yl_birth varchar(100) not null comment '阴历生日',
remark varchar(100) null comment '备注',
is_delete int default 0 null comment '0:正常 1:删除'
)
comment '生日';
然后,将需要提醒用户的姓名、农历生日等数据写入
PS:这里阴历生日格式是 mm-dd,比如:10-25
1-3 查询数据
import pymysql
class Birth(object):
def __init__(self):
self.db = pymysql.connect(host='**',
user='root',
password='**',
database='xag')
self.cursor = self.db.cursor()
def __get_births(self):
# 获取所有数据
self.cursor.execute("""
select name,yl_birth,remark from birthday where is_delete=0;""")
datas = list(self.cursor.fetchall())
1-4 遍历,获取距离今天的天数
遍历上面的数据,将阴历转为阳历,然后计算出距离今天的天数
from zhdate import ZhDate
...
def __get_diff(self, birth):
"""
根据农历生日,获取当前日期距离的时间(天)
:param birth: 农历生日,格式:10-25
:return:
"""
# 1、获取今日的农历日历
now = str(datetime.now().strftime('%Y-%m-%d')).split("-")
# 年、月、日
year, month, day = int(now[0]), int(now[1]), int(now[2])
# 1、获取阴历生日,转为阳历
birth_month = int(birth.split("-")[0].strip())
birth_day = int(birth.split("-")[-1].strip())
birth_ying = ZhDate(year, birth_month, birth_day)
# 转为阳历
birth_yang = birth_ying.to_datetime()
# 2、计算距离当前日期的时间间隔(天)
today = datetime.now().strftime('%Y-%m-%d')
d1 = datetime.strptime(today, '%Y-%m-%d')
diff_day = (birth_yang-d1).days
return diff_day
...
# 遍历数据
for item in datas:
name = item[0]
birth = item[1]
nickname = item[2]
diff = self.__get_diff(birth)
...
1-5 组装数据及消息推送
通过时间间隔,在提前一周、生日当天做一个提醒
最后,将组装好的消息通过企业微信机器人发送出去
import requests
import json
...
def send_wechat(self, msg: str):
"""发送信息到企业微信"""
# 这里填写你的机器人的webhook链接
url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key**'
headers = {"Content-Type": "text/plain"}
data = {
"msgtype": "text",
"text": {
"content": msg
}
}
# 发送消息
requests.post(url, headers=headers, data=json.dumps(data))
...
来源:https://mp.weixin.qq.com/s/CKscpZObQQKXdhmQFGF1ug
标签:Python,生日,提醒
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Tableau连接mysql数据库的实现步骤
2024-01-21 20:50:41
![](https://img.aspxhome.com/file/2023/1/74781_0s.jpg)
vue之组件内监控$store中定义变量的变化详解
2023-07-02 17:01:31
一篇文章介绍redux、react-redux、redux-saga总结
2023-08-22 16:56:32
![](https://img.aspxhome.com/file/2023/2/55972_0s.jpg)
python 制作网站小说下载器
2021-06-07 23:04:42
![](https://img.aspxhome.com/file/2023/4/64984_0s.png)
mysql之innodb的锁分类介绍
2024-01-27 00:50:10
python实战之德州扑克第三步-比较大小
2022-01-01 22:27:11
Python制作exe文件简单流程
2022-06-07 01:11:12
![](https://img.aspxhome.com/file/2023/2/91852_0s.png)
python超参数优化的具体方法
2022-01-04 22:28:31
发个js从样式表取值的函数
2008-05-20 12:23:00
基于事件冒泡、事件捕获和事件委托详解
2024-04-28 09:43:33
![](https://img.aspxhome.com/file/2023/2/132812_0s.jpg)
Mysql获取当前日期的前几天日期的方法
2024-01-16 01:22:31
Python实现人脸识别
2022-10-01 06:06:24
![](https://img.aspxhome.com/file/2023/9/80809_0s.png)
解决python3插入mysql时内容带有引号的问题
2024-01-24 13:34:09
Python3 xml.etree.ElementTree支持的XPath语法详解
2021-08-06 07:29:44
bak文件怎么打开 2000w数据怎么打开?
2024-01-12 19:30:43
![](https://img.aspxhome.com/file/2023/0/96200_0s.jpg)
vue 指令版富文本溢出省略截取示例详解
2024-05-28 15:52:36
![](https://img.aspxhome.com/file/2023/2/123152_0s.png)
详解python 利用echarts画地图(热力图)(世界地图,省市地图,区县地图)
2021-10-24 06:59:30
![](https://img.aspxhome.com/file/2023/9/135249_0s.png)
如何查看python中安装库的文件位置
2021-04-17 04:09:31
![](https://img.aspxhome.com/file/2023/9/85039_0s.png)
python中的随机数种子seed()用法说明
2021-11-15 17:16:34
![](https://img.aspxhome.com/file/2023/4/92334_0s.png)
Python 虚拟环境工作原理解析
2023-02-21 02:18:50