網站首頁 編程語言 正文
在國內,大部分人都是過農歷生日,然后借助日歷工具獲取農歷日期對應的陽歷日期,以這一天來過生!
這里還有一個痛點,即:每一年的農歷生日對應的陽歷日期都不一樣
本篇文章將教你利用 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
- 上一篇:C語言中的冒泡排序問題_C 語言
- 下一篇:C語言多文件編程問題解析_C 語言
相關推薦
- 2022-11-17 解讀Python中字典的key都可以是什么_python
- 2022-06-09 Nginx速查手冊及常見問題_nginx
- 2023-04-01 一文初探?Goroutine?與?channel基本用法_Golang
- 2023-03-01 kotlin?object關鍵字單例模式實現示例詳解_Android
- 2022-09-25 MongoDB聚合管道 $lookup 與$mergeObjects配合使用 以及使用let,pip
- 2022-11-14 python流程控制語句
- 2022-06-04 C#?XML基礎入門小結(XML文件內容增刪改查清)_C#教程
- 2022-04-10 python中的單向鏈表實現_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支