python scrapy項(xiàng)目下spiders內(nèi)多個(gè)爬蟲(chóng)同時(shí)運(yùn)行的實(shí)現(xiàn)
一般創(chuàng)建了scrapy文件夾后,可能需要寫(xiě)多個(gè)爬蟲(chóng),如果想讓它們同時(shí)運(yùn)行而不是順次運(yùn)行的話(huà),得怎么做?

a、在spiders目錄的同級(jí)目錄下創(chuàng)建一個(gè)commands目錄,并在該目錄中創(chuàng)建一個(gè)crawlall.py,將scrapy源代碼里的commands文件夾里的crawl.py源碼復(fù)制過(guò)來(lái),只修改run()方法即可!
import os
from scrapy.commands import ScrapyCommand
from scrapy.utils.conf import arglist_to_dict
from scrapy.utils.python import without_none_values
from scrapy.exceptions import UsageError
class Command(ScrapyCommand):
requires_project = True
def syntax(self):
return "[options] <spider>"
def short_desc(self):
return "Run all spider"
def add_options(self, parser):
ScrapyCommand.add_options(self, parser)
parser.add_option("-a", dest="spargs", action="append", default=[], metavar="NAME=VALUE",
help="set spider argument (may be repeated)")
parser.add_option("-o", "--output", metavar="FILE",
help="dump scraped items into FILE (use - for stdout)")
parser.add_option("-t", "--output-format", metavar="FORMAT",
help="format to use for dumping items with -o")
def process_options(self, args, opts):
ScrapyCommand.process_options(self, args, opts)
try:
opts.spargs = arglist_to_dict(opts.spargs)
except ValueError:
raise UsageError("Invalid -a value, use -a NAME=VALUE", print_help=False)
if opts.output:
if opts.output == '-':
self.settings.set('FEED_URI', 'stdout:', priority='cmdline')
else:
self.settings.set('FEED_URI', opts.output, priority='cmdline')
feed_exporters = without_none_values(
self.settings.getwithbase('FEED_EXPORTERS'))
valid_output_formats = feed_exporters.keys()
if not opts.output_format:
opts.output_format = os.path.splitext(opts.output)[1].replace(".", "")
if opts.output_format not in valid_output_formats:
raise UsageError("Unrecognized output format '%s', set one"
" using the '-t' switch or as a file extension"
" from the supported list %s" % (opts.output_format,
tuple(valid_output_formats)))
self.settings.set('FEED_FORMAT', opts.output_format, priority='cmdline')
def run(self, args, opts):
#獲取爬蟲(chóng)列表
spd_loader_list=self.crawler_process.spider_loader.list()#獲取所有的爬蟲(chóng)文件。
print(spd_loader_list)
#遍歷各爬蟲(chóng)
for spname in spd_loader_list or args:
self.crawler_process.crawl(spname, **opts.spargs)
print ('此時(shí)啟動(dòng)的爬蟲(chóng)為:'+spname)
self.crawler_process.start()
b、還得在里面加個(gè)_init_.py文件

c、到這里還沒(méi)完,settings.py配置文件還需要加一條。
COMMANDS_MODULE = ‘項(xiàng)目名稱(chēng).目錄名稱(chēng)'
COMMANDS_MODULE = 'ds1.commands'
d、最后啟動(dòng)crawlall即可!
當(dāng)然,安全起見(jiàn),可以先在命令行中進(jìn)入該項(xiàng)目所在目錄,并輸入scrapy -h,可以查看是否有命令crawlall 。如果有,那就成功了,可以啟動(dòng)了
我是寫(xiě)了個(gè)啟動(dòng)文件,放在第一級(jí)即可

要不直接在命令臺(tái)cmd里輸入 scrapy crawlall 就行了
##注意的是,爬蟲(chóng)好像是2個(gè)同時(shí)運(yùn)行,而且運(yùn)行時(shí)是交叉的?
還有settings里的文件,只針對(duì)其中一個(gè)?
到此這篇關(guān)于python scrapy項(xiàng)目下spiders內(nèi)多個(gè)爬蟲(chóng)同時(shí)運(yùn)行的文章就介紹到這了,更多相關(guān)python scrapy項(xiàng)目下spiders內(nèi)多個(gè)爬蟲(chóng)同時(shí)運(yùn)行內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python scrapy拆解查看Spider類(lèi)爬取優(yōu)設(shè)網(wǎng)極細(xì)講解
- Python如何獲取pid和進(jìn)程名字
- Python中rapidjson參數(shù)校驗(yàn)實(shí)現(xiàn)
- Python爬蟲(chóng)框架之Scrapy中Spider的用法
- python爬蟲(chóng)scrapy基于CrawlSpider類(lèi)的全站數(shù)據(jù)爬取示例解析
- Python中Pyspider爬蟲(chóng)框架的基本使用詳解
- Python爬蟲(chóng)Scrapy框架CrawlSpider原理及使用案例
- Python爬蟲(chóng)之Spider類(lèi)用法簡(jiǎn)單介紹
- PID原理與python的簡(jiǎn)單實(shí)現(xiàn)和調(diào)參
相關(guān)文章
在.NET中使用Newtonsoft.Json轉(zhuǎn)換,讀取,寫(xiě)入的方法介紹
Newtonsoft.Json.JsonConvert類(lèi)是非微軟提供的一個(gè)JSON序列化和反序列的開(kāi)源免費(fèi)的類(lèi)庫(kù)2012-08-08
vb.net 數(shù)組參與SQL語(yǔ)句的查詢(xún)范例
做了一個(gè)項(xiàng)目,然后遇到這樣的一個(gè)問(wèn)題自己寫(xiě)了這個(gè)方法,覺(jué)得很實(shí)用就貼上來(lái)分享一下吧2013-04-04
asp.net下降文本格式數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫(kù)中的代碼
導(dǎo)入文本格式數(shù)據(jù)到數(shù)據(jù)庫(kù)中的代碼,需要的朋友可以參考下。2010-05-05
完美兼容ie和firefox的asp.net網(wǎng)站加入收藏和設(shè)置主頁(yè)
這篇文章主要介紹了完美兼容ie和firefox的asp.net網(wǎng)站加入收藏和設(shè)置主頁(yè),需要的朋友可以參考下2014-12-12
ASP.NET 緩存分析和實(shí)踐淺析提高運(yùn)行效率
說(shuō)到ASP.NET緩存,那就是:盡早緩存;經(jīng)常緩存您應(yīng)該在應(yīng)用程序的每一層都實(shí)現(xiàn)緩存。2010-02-02
.NET生成動(dòng)態(tài)驗(yàn)證碼的完整步驟
這篇文章主要給大家介紹了關(guān)于.NET生成動(dòng)態(tài)驗(yàn)證碼的完整步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用.NET具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07

