前几日,爬虫基本能爬点东西出来了,现在需要实现定时把数据爬到DB里去,可以使用windows定时任务执行py脚本,但好像不彻底,要做一个纯(jiao)粹(qing)的程序员,定时任务的重任落到了Aspshceduler上;下文中许多内容来自互联网。
APScheduler是基于Quartz的一个Python定时任务框架,实现了Quartz的所有功能,使用起来十分方便。提供了基于日期、固定时间间隔以及crontab类型的任务,并且可以持久化任务。基于这些功能,我们可以很方便的实现一个python定时任务系统,写python还是要比java舒服多了。
使用 easy_install apscheduler安装;
APScheduler提供了jobstore用于存储job的执行信息,默认使用的是RAMJobStore,还提供了SQLAlchemyJobStore、ShelveJobStore和MongoDBJobStore。APScheduler允许同时使用多个jobstore,通过别名(alias)区分,在添加job时需要指定具体的jobstore的别名,否则使用的是别名是default的jobstore,即RAMJobStore。
安装MongoDBJobStore时注意,安装Mongo时应注意版本,否则报错,新版的Mongo for python包,没有connect方法;使用2.0版本后正常; pip uninstall mongo; pip install mongo==2.0
下面以MongoDBJobStore举例说明。
1 import pymongo
2
from apscheduler.scheduler
import Scheduler
3 from apscheduler.jobstores.mongodb_store import MongoDBJobStore
4 import time
5
6 sched = Scheduler(daemonic = False)
7
8 mongo = pymongo.Connection(host= ' 127.0.0.1 ', port=27017)
9 store = MongoDBJobStore(connection=mongo)
10 sched.add_jobstore(store, ' mongo ') # 别名是mongo
11
12 @sched.cron_schedule(second= ' * ', day_of_week= ' 0-4 ', hour= ' 9-12,13-15 ', jobstore= ' mongo ') # 向别名为mongo的jobstore添加job
13 def job():
14 print ' a job '
15 time.sleep(1)
16
17 sched.start()
3 from apscheduler.jobstores.mongodb_store import MongoDBJobStore
4 import time
5
6 sched = Scheduler(daemonic = False)
7
8 mongo = pymongo.Connection(host= ' 127.0.0.1 ', port=27017)
9 store = MongoDBJobStore(connection=mongo)
10 sched.add_jobstore(store, ' mongo ') # 别名是mongo
11
12 @sched.cron_schedule(second= ' * ', day_of_week= ' 0-4 ', hour= ' 9-12,13-15 ', jobstore= ' mongo ') # 向别名为mongo的jobstore添加job
13 def job():
14 print ' a job '
15 time.sleep(1)
16
17 sched.start()
注意start必须在添加job动作之后调用,否则会抛错。默认会把job信息保存在apscheduler数据库下的jobs表:
> db.jobs.findOne()
{
" _id " : ObjectId( " 502202d1443c1557fa8b8d66 "),
" runs " : 20,
" name " : " job ",
" misfire_grace_time " : 1,
" coalesce " : true,
" args " : BinData(0, " gAJdcQEu "),
" next_run_time " : ISODate( " 2012-08-08T14:10:46Z "),
" max_instances " : 1,
" max_runs " : null,
" trigger " : BinData(0, " gAJjYXBzY2hlZHVsZXIudHJpZ2dlcnMuY3JvbgpDcm9uVHJpZ2dlcgpxASmBcQJ9cQMoVQZmaWVsZHNxBF1xBShjYXBzY2hlZHVsZXIudHJpZ2dlcnMuY3Jvbi5maWVsZHMKQmFzZUZpZWxkCnEGKYFxB31xCChVCmlzX2RlZmF1bHRxCYhVC2V4cHJlc3Npb25zcQpdcQtjYXBzY2hlZHVsZXIudHJpZ2dlcnMuY3Jvbi5leHByZXNzaW9ucwpBbGxFeHByZXNzaW9uCnEMKYFxDX1xDlUEc3RlcHEPTnNiYVUEbmFtZXEQVQR5ZWFycRF1YmgGKYFxEn1xEyhoCYhoCl1xFGgMKYFxFX1xFmgPTnNiYWgQVQVtb250aHEXdWJjYXBzY2hlZHVsZXIudHJpZ2dlcnMuY3Jvbi5maWVsZHMKRGF5T2ZNb250aEZpZWxkCnEYKYFxGX1xGihoCYhoCl1xG2gMKYFxHH1xHWgPTnNiYWgQVQNkYXlxHnViY2Fwc2NoZWR1bGVyLnRyaWdnZXJzLmNyb24uZmllbGRzCldlZWtGaWVsZApxHymBcSB9cSEoaAmIaApdcSJoDCmBcSN9cSRoD05zYmFoEFUEd2Vla3EldWJjYXBzY2hlZHVsZXIudHJpZ2dlcnMuY3Jvbi5maWVsZHMKRGF5T2ZXZWVrRmllbGQKcSYpgXEnfXEoKGgJiWgKXXEpY2Fwc2NoZWR1bGVyLnRyaWdnZXJzLmNyb24uZXhwcmVzc2lvbnMKUmFuZ2VFeHByZXNzaW9uCnEqKYFxK31xLChoD05VBGxhc3RxLUsEVQVmaXJzdHEuSwB1YmFoEFULZGF5X29mX3dlZWtxL3ViaAYpgXEwfXExKGgJiWgKXXEyKGgqKYFxM31xNChoD05oLUsMaC5LCXViaCopgXE1fXE2KGgPTmgtSw9oLksNdWJlaBBVBGhvdXJxN3ViaAYpgXE4fXE5KGgJiGgKXXE6aAwpgXE7fXE8aA9Oc2JhaBBVBm1pbnV0ZXE9dWJoBimBcT59cT8oaAmJaApdcUBoDCmBcUF9cUJoD05zYmFoEFUGc2Vjb25kcUN1YmVVCnN0YXJ0X2RhdGVxRE51Yi4= "),
" func_ref " : " __main__:job ",
" kwargs " : BinData(0, " gAJ9cQEu ")
{
" _id " : ObjectId( " 502202d1443c1557fa8b8d66 "),
" runs " : 20,
" name " : " job ",
" misfire_grace_time " : 1,
" coalesce " : true,
" args " : BinData(0, " gAJdcQEu "),
" next_run_time " : ISODate( " 2012-08-08T14:10:46Z "),
" max_instances " : 1,
" max_runs " : null,
" trigger " : BinData(0, " gAJjYXBzY2hlZHVsZXIudHJpZ2dlcnMuY3JvbgpDcm9uVHJpZ2dlcgpxASmBcQJ9cQMoVQZmaWVsZHNxBF1xBShjYXBzY2hlZHVsZXIudHJpZ2dlcnMuY3Jvbi5maWVsZHMKQmFzZUZpZWxkCnEGKYFxB31xCChVCmlzX2RlZmF1bHRxCYhVC2V4cHJlc3Npb25zcQpdcQtjYXBzY2hlZHVsZXIudHJpZ2dlcnMuY3Jvbi5leHByZXNzaW9ucwpBbGxFeHByZXNzaW9uCnEMKYFxDX1xDlUEc3RlcHEPTnNiYVUEbmFtZXEQVQR5ZWFycRF1YmgGKYFxEn1xEyhoCYhoCl1xFGgMKYFxFX1xFmgPTnNiYWgQVQVtb250aHEXdWJjYXBzY2hlZHVsZXIudHJpZ2dlcnMuY3Jvbi5maWVsZHMKRGF5T2ZNb250aEZpZWxkCnEYKYFxGX1xGihoCYhoCl1xG2gMKYF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