zoukankan      html  css  js  c++  java
  • pyinstaller,scrapy和apscheduler

    一、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自带的“任务计划程序”,还是很好用的,创建基本任务就可以了。

  • 相关阅读:
    #ASP.NET Core 1.0 Key Features
    #asp.net core mvc 的视图注入
    # asp.net core 1.0 项目结构
    dotnet core 初试两个小问题解决
    1044 火星数字(20 分)
    1043 输出PATest(20 分)
    1042 字符统计(20 分)
    1041 考试座位号(15 分)
    1040 有几个PAT(25 分)
    1039 到底买不买(20 分)
  • 原文地址:https://www.cnblogs.com/jdbc2nju/p/9563761.html
Copyright © 2011-2022 走看看