如何使用py2exe将我的scrapy蜘蛛构建到exe文件?

Myz*_*yzh 5 python exe py2exe scrapy

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

当我按照py2exe的说明操作时,我在同一文件夹中创建一个新文件"Setup.py",其中包含以下内容:

from distutils.core import setup
import py2exe

setup(console = ["spider_us.py"])
Run Code Online (Sandbox Code Playgroud)

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

怎么可能构建整个蜘蛛程序(当我使用"scrapy startproject XXX"时通过scrapy自动创建)到exe文件,而不仅仅是"spiders"子文件夹中的蜘蛛文件(在我的例子中是"spider_us.py") .

任何人给出一些建议或帮助,欢迎任何评论.非常感谢.

sta*_*ify 3

尝试通过 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
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅“从脚本运行 Scrapy”的文档