【问题标题】:how to build my scrapy spider to an exe file using py2exe?如何使用 py2exe 将我的 scrapy spider 构建为 exe 文件?
【发布时间】:2013-10-18 02:02:55
【问题描述】:

我使用scrapy创建了一个项目,并在“spiders”文件夹中添加了我自己的蜘蛛,比如“spider_us.py”,我想构建一个可以在其他计算机上执行的exe文件,而无需安装scrapy。

当我按照 py2exe 的说明操作时,我在同一文件夹中创建了一个新文件“Setup.py”,内容如下:

from distutils.core import setup
import py2exe

setup(console = ["spider_us.py"])

但是,它不起作用,因为当我运行我的蜘蛛时,我使用命令“scrapy crawl spider_us”而不是直接运行“spiders”文件夹中的文件“spider_us.py”。

如何将整个蜘蛛程序(当我使用“scrapy startproject XXX”时由scrapy自动创建)构建到一个exe文件,而不仅仅是“蜘蛛文件”中的“spider_us.py”)蜘蛛”子文件夹。

任何人提供一些建议或帮助,欢迎任何评论。非常感谢。

【问题讨论】:

    标签: python exe scrapy py2exe


    【解决方案1】:

    尝试通过 Python 脚本(而不是命令 scrapy crawl <spider_name>)运行蜘蛛。您需要编写一些代码,例如:

    from twisted.internet import reactor
    from scrapy.crawler import Crawler
    from scrapy import log, signals
    from testspiders.spiders.followall import FollowAllSpider
    from scrapy.utils.project import get_project_settings
    
    spider = FollowAllSpider(domain='scrapinghub.com')
    settings = get_project_settings()
    crawler = Crawler(settings)
    crawler.signals.connect(reactor.stop, signal=signals.spider_closed)
    crawler.configure()
    crawler.crawl(spider)
    crawler.start()
    log.start()
    reactor.run() # the script will block here until the spider_closed signal was sent
    

    详情见the documentations on "Run Scrapy from a script"

    【讨论】:

      猜你喜欢
      • 2014-06-14
      • 1970-01-01
      • 1970-01-01
      • 2017-09-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多