安装

pip install apscheduler

简单dome

监控产品经理微博是否跟新,如更新发送邮件消息,钉钉消息

from datetime import datetime

from apscheduler.schedulers.blocking import BlockingScheduler

from utils.ding_talk import ding_talk
from utils.setting import ORDER_RECEIVER
from utils.send_email import SendEmail
from spider.spider import weibo

def Timing():
    weibo()  

if __name__ == '__main__':
    scheduler = BlockingScheduler()
    # scheduler.add_job(Timing, 'interval', seconds=3)     # 每3秒执行一次
    scheduler.add_job(Timing, 'cron', minute="*/2", id='Timing')  # 每两分钟执行一次
    try:
        scheduler.start()
    except (KeyboardInterrupt, SystemExit):
        ding_talk()   # 失败发送钉钉消息
        mail_data = f'''
                定时监控任务失败\n
                请查看验证!\n
                发送时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n
                '''
        SendEmail().send_mail(receive=ORDER_RECEIVER, mail_data=mail_data)

如果报错 skipped: maximum number of running instances reached (1) 程序运行上个任务还没运行完成,下个任务已经开始,产生此错误。办法修改演示执行间隔
如果还没错: 请查看