【问题标题】:How do I package a Scrapy script into a standalone application?如何将 Scrapy 脚本打包到独立的应用程序中?
【发布时间】:2013-09-03 03:57:27
【问题描述】:

我有一套 Scrapy 蜘蛛。它们需要每天从桌面应用程序运行。 在另一台 Windows 机器上安装和运行它的最简单方法是什么(从用户的角度来看)?

【问题讨论】:

    标签: python scrapy desktop-application py2exe pyinstaller


    【解决方案1】:

    创建一个将scrapy crawl <spider_name> 作为系统命令运行的脚本(例如run_spider.py)。

    run_spider.py

    from os import system
    output_file_name = 'results.csv'
    system('scrapy crawl myspider -o ' + output_file_name + ' -t csv')
    

    然后将该脚本提供给 PyInstaller:

    pyinstaller run_spider.py
    

    【讨论】:

      【解决方案2】:

      我猜最简单的方法是用python为他们编写一个脚本......

      如果您运行的是 Windows Server,您甚至可以安排您使用的命令(scrapy crawl yoursprider)来运行蜘蛛。

      【讨论】:

        【解决方案3】:

        这是另一种将蜘蛛作为独立脚本或可执行文件运行的可能性

        import scrapy
        from scrapy.crawler import CrawlerProcess
        
        class MySpider(scrapy.Spider):
            # Your spider definition
            ...
        
        process = CrawlerProcess({
            'USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)'
        })
        
        process.crawl(MySpider)
        process.start() # the script will block here until the crawling is finished
        

        您可以在这里找到更多信息:https://doc.scrapy.org/en/1.0/topics/practices.html

        【讨论】:

          猜你喜欢
          • 2013-04-24
          • 1970-01-01
          • 2016-06-08
          • 1970-01-01
          • 1970-01-01
          • 2011-04-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多