python - 如何从另一个 Python 脚本调用特定的 Scrapy 蜘蛛

标签 python scrapy

我有一个名为 algorithm.py 的脚本,我希望能够在脚本期间调用 Scrapy 蜘蛛。文件结构为:

算法.py 我的蜘蛛/

其中 MySpiders 是包含多个 scrapy 项目的文件夹。我想创建方法 perform_spider1()、perform_spider2()... 我可以在 algorithm.py 中调用它们。

我如何构建这个方法?

我已经设法使用以下代码调用一个蜘蛛,但是,这不是一种方法,它只适用于一个蜘蛛。我是初学者,需要帮助!

import sys,os.path
sys.path.append('path to spider1/spider1')
from twisted.internet import reactor
from scrapy.crawler import Crawler
from scrapy.settings import Settings
from scrapy import log, signals
from scrapy.xlib.pydispatch import dispatcher
from spider1.spiders.spider1_spider import Spider1Spider

def stop_reactor():
    reactor.stop()

dispatcher.connect(stop_reactor, signal=signals.spider_closed)

spider = RaListSpider()
crawler = Crawler(Settings())
crawler.configure()
crawler.crawl(spider)
crawler.start()
log.start()
log.msg('Running reactor...')
reactor.run() # the script will block here
log.msg('Reactor stopped.')

最佳答案

只需检查您的蜘蛛并通过调用configurecrawlstart 来设置它们,然后才调用log。 start()reactor.run()。并且 scrapy 会在同一个进程中运行多个爬虫。

有关详细信息,请参阅 documentationthis thread .

此外,考虑通过 scrapyd 运行您的蜘蛛程序.

希望对您有所帮助。

关于python - 如何从另一个 Python 脚本调用特定的 Scrapy 蜘蛛,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16994768/

相关文章:

python - 使用 Scrapy shell 抓取 JSON 文件时终端窗口自动关闭

python - 如何让 Scrapy 使用 Python 2.7.10 而不是 3.4.3

python - Scrapy 蜘蛛内存泄漏

python - 从脚本设置 Scrapy start_urls

python - 如何使用 python-binance : APIError(code=-1111): Precision is over the maximum defined for this asset 下 future 市场订单

python - 使用 Beautifulsoup 的带有空格的类的正则表达式

c# - 用于构建自动化的 powershell 或 python

python - 如何让 python 等到 Excel 宏/刷新完成

python - 从 python 执行 .jar 文件

python - scrapy:蜘蛛中的一个小 "spider"?