1. 5 分鐘,教你用 Python 制作一個(gè)生日提醒!

        共 6166字,需瀏覽 13分鐘

         ·

        2022-12-31 00:41

        大家好,我是安果!

        在國內(nèi),大部分人都是過農(nóng)歷生日,然后借助日歷工具獲取農(nóng)歷日期對(duì)應(yīng)的陽歷日期,以這一天來過生!

        這里還有一個(gè)痛點(diǎn),即:每一年的農(nóng)歷生日對(duì)應(yīng)的陽歷日期都不一樣

        本篇文章將教你利用 Python 制作一個(gè)簡單的生日提醒

        1. 實(shí)戰(zhàn)

        具體操作步驟如下

        1-1  安裝依賴

        # 安裝依賴
        pip3 install zhdate

        pip3 install pymysql

        其中,zhdate 模塊用于中國農(nóng)歷、陽歷之間的轉(zhuǎn)換,并且支持日期差額計(jì)算

        項(xiàng)目地址:

        https://github.com/CutePandaSh/zhdate

        1-2  創(chuàng)建數(shù)據(jù)表

        創(chuàng)建一條數(shù)據(jù)表

        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 '生日';

        然后,需要提醒用戶的姓名、農(nóng)歷生日等數(shù)據(jù)寫入

        PS:這里陰歷生日格式是 mm-dd,比如:10-25

        1-3  查詢數(shù)據(jù)

        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):
                # 獲取所有數(shù)據(jù)
                self.cursor.execute("""
                                     select name,yl_birth,remark from birthday where is_delete=0;"""
        )

                datas = list(self.cursor.fetchall())

        1-4  遍歷,獲取距離今天的天數(shù)

        遍歷上面的數(shù)據(jù),將陰歷轉(zhuǎn)為陽歷,然后計(jì)算出距離今天的天數(shù)

        from zhdate import ZhDate

        ...
          def __get_diff(self, birth):
                """
                根據(jù)農(nóng)歷生日,獲取當(dāng)前日期距離的時(shí)間(天)
                :param birth: 農(nóng)歷生日,格式:10-25
                :return:
                """

                # 1、獲取今日的農(nóng)歷日歷
                now = str(datetime.now().strftime('%Y-%m-%d')).split("-")
                # 年、月、日
                year, month, day = int(now[0]), int(now[1]), int(now[2])

                # 1、獲取陰歷生日,轉(zhuǎn)為陽歷
                birth_month = int(birth.split("-")[0].strip())
                birth_day = int(birth.split("-")[-1].strip())
                birth_ying = ZhDate(year, birth_month, birth_day)

                # 轉(zhuǎn)為陽歷
                birth_yang = birth_ying.to_datetime()

                # 2、計(jì)算距離當(dāng)前日期的時(shí)間間隔(天)
                today = datetime.now().strftime('%Y-%m-%d')
                d1 = datetime.strptime(today, '%Y-%m-%d')

                diff_day = (birth_yang-d1).days
                return diff_day

        ...
         # 遍歷數(shù)據(jù)
                for item in datas:
                    name = item[0]
                    birth = item[1]
                    nickname = item[2]
                    diff = self.__get_diff(birth)
        ...

        1-5  組裝數(shù)據(jù)及消息推送

        通過時(shí)間間隔,在提前一周、生日當(dāng)天做一個(gè)提醒

        最后,將組裝好的消息通過企業(yè)微信機(jī)器人發(fā)送出去

        import requests
        import json

        ...
           def send_wechat(self, msg: str):
                """發(fā)送信息到企業(yè)微信"""
                # 這里填寫你的機(jī)器人的webhook鏈接
                url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key**'
                headers = {"Content-Type""text/plain"}
                data = {
                    "msgtype""text",
                    "text": {
                        "content": msg
                    }
                }
                # 發(fā)送消息
                requests.post(url, headers=headers, data=json.dumps(data))
        ...

        2. 最后

        我已經(jīng)將文中的源碼上傳到公眾號(hào)后臺(tái),回復(fù)關(guān)鍵字 birth 獲取完整的源碼!


        推薦閱讀

        5 分鐘,教你從零快速編寫一個(gè)油猴腳本!

        爬蟲工具篇 - 必會(huì)用的 6 款 Chrome 插件

        小技巧 | Chrome 插件如何完成剪切板的操作!


        END


        好文和朋友一起看~

        瀏覽 89
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
          
          

            1. 午夜精品一区二区三区在线视频99 | 来个欧美操逼的 | 在线观看无码AV | 日本在线视频一区二区 | 中国毛片网 |