python实现简单的定时任务

时间:2023-04-17 17:42:20

1.首先安装 schedule 模块

命令行安装

pip install schedule

pyCharm编辑器安装

File->setting->project:youProject->Project Interpreter->点+号->搜索->install按钮

2.代码

# coding:utf8

from bs4 import BeautifulSoup
import schedule
import time
import urllib2

def job():
print ("I'm working")

schedule.every().minutes.do(job)

while True:
schedule.run_pending()
time.sleep(1)

时间段选择

schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
schedule.every(5).to(10).days.do(job)
schedule.every().monday.do(job)
schedule.every().wednesday.at("13:15").do(job)