在add_job
中添加参数day_of_week
即可:
- day_of_week = "0"表示:只有周一运行
- day_of_week = "0-4"表示:周一到周五运行
- day_of_week = "0,1,2"表示:周一二三运行
示例程序
from datetime import datetimefrom apscheduler.schedulers.background import BlockingSchedulerdef auto_run():print("运行程序")print(datetime.now())def main():print("测试周N自动运行")scheduler = BlockingScheduler(timezone='Asia/Shanghai')scheduler.add_job(auto_run, 'cron', hour=10, minute=26, day_of_week='0', misfire_grace_time=60 * 60,max_instances=3) # 0 表示周一scheduler.start()if __name__ == '__main__':main()
最后效果: