石家庄网站建设哪家专业,平台设计网站公司电话,排名网站建设,龙岩网站建设一般处理scrapy中包括多个pipeline时如何让spider执行制定的pipeline管道#xff11;:创建一个装饰器from scrapy.exceptions import DropItemimport functools当有多个pipeline时,判断spider如何执行指定的管道 def check_spider_pipeline(process_item_method): functools.wr… 处理scrapy中包括多个pipeline时如何让spider执行制定的pipeline管道:创建一个装饰器from scrapy.exceptions import DropItemimport functools当有多个pipeline时,判断spider如何执行指定的管道 def check_spider_pipeline(process_item_method): functools.wraps(process_item_method) def wrapper(self, item, spider): # message template for debugging msg %%s %s pipeline step % (self.__class__.__name__,) if self.__class__ in spider.pipeline:#判断要执行的spider中是否包含所需的pipeline 如果有则执行否则抛出DropItem信息 spider.logger.debug(msg % executing) return process_item_method(self,item,spider) # otherwise, just return the untouched item (skip this step in # the pipeline) else: spider.logger.debug(msg % skipping) raise DropItem(Missing pipeline property) return wrapper2:在每个spider所在的类中添加一个pipeline数组里面包含要执行的pipeline的名字 -*- coding: utf-8 -*-from scrapy.spiders import CrawlSpider,Rule# from scrapy.selector import Selectorfrom ..items import BotcnblogsItem,BotItemfrom scrapy.linkextractors import LinkExtractorimport refrom ..BotcnblogsPipeline import BotcnblogsPipelineclass CnblogsSpider(CrawlSpider): pipeline set([BotcnblogsPipeline,]) #爬虫名称 name cnblogs #设置允许的域名 allowed_domains [cnblogs.com] #设置开始爬去的页面 start_urls ( http://www.cnblogs.com/fengzheng/, ) rules( Rule(LinkExtractor(allow(fengzheng/default.html\?page\([\d]))),callbackparse_item,followTrue),# Rule(LinkExtractor(allow(fengzheng/p/([\d]).html)),callbackparse_info,followTrue), ) 3:在要执行的pipeline中的process_item方法加上装饰器这样就可以过滤pipeline了import jsonfrom .checkpipe import check_spider_pipeline class BotcnblogsPipeline(object): def __init__(self): self.fileopen(jd.json,w) check_spider_pipeline def process_item(self,item,spider): #此处如果有中文的话要加上ensure_asciiFalse参数否则可能出现乱码 recordjson.dumps(dict(item),ensure_asciiFalse)\n self.file.write(record) return item def open_spider(self,spider): print(打开爬虫了) def close_spider(self,spider): print(关闭爬虫) self.file.close() 具体例子可以参考其中的cnblogs spider的例子 下载 转载于:https://www.cnblogs.com/fly-kaka/p/5216791.html