一、scrapy拉起方式
1. 簡單cmd拉起
from scrapy.cmdline import execute spiders = [ 'scrapy crawl liepin', 'scrapy crawl lagou' ] if __name__ == '__main__': for i in spiders: execute(i.split())
2. subprocess拉起
subprocess.Popen('scrapy crawl aws_ec2_ondemand')
3. 調用內部方法拉起
process = CrawlerProcess(get_project_settings()) process.crawl('aws_ec2_ondemand') process.start()
from scrapy.commands import ScrapyCommand from scrapy.utils.project import get_project_settings class Command(ScrapyCommand): requires_project = True def run(self,args,opts): spiders_list = self.crawler_process.spiders.list() for name in spiders_list: self.crawler_process.crawl(name,**opts.__dict__) self.crawler_process.start()
二、apschedular總結
1. 基於Quartz,有四個組成部分:trigger,job,scheduler,executer
2. cron表達式
3. 常用的調度器
- BlockingScheduler:僅可用在當前你的進程之內,與當前的進行共享計算資源
- BackgroundScheduler: 在后台運行調度,不影響當前的系統計算運行
- AsyncIOScheduler: 如果當前系統中使用了async module,則需要使用異步的調度器
- GeventScheduler: 如果使用了gevent,則需要使用該調度
- TornadoScheduler: 如果使用了Tornado, 則使用當前的調度器
- TwistedScheduler:Twister應用的調度器
- QtScheduler: Qt的調度器
4. python內置的可實現定時任務的模塊:timer和sche
三、遇到的問題
1. 如果用scheduler調度爬蟲,拉起scrapy的方式只能用subprocess, 否則會報錯“signal只能在主進程使用”。
2. 用pyinstaller打包程序,想要在沒有安裝環境的windows運行的話,拉起scrapy只能用內部方法拉起,因為
用命令拉起的話,這些命令只有安裝了環境才能用。包括subprocess也是只能調用命令拉起,所以也不能用。
3. 綜合1、2點,那么如果用pyinstyaller打包程序,就不能用scheduler定時任務了。
4. 最終決定將定時任務的功能交給windows自帶的“任務計划程序”,還是很好用的,創建基本任務就可以了。
