zoukankan      html  css  js  c++  java
  • scrapy Item Loader机制

    scrapy item loader机制

    直接赋值取值的方式,会出现一下几个问题
    1. 代码量一多,各种css和xpath选择器,充斥整个代码逻辑,没有规则,可读性差、不利于维护
    2. 对于一个字段的预处理,不明确,也不应该出现在主逻辑中

    这时通过scrapy中的ItemLoader模块来处理。

    ItemLoader对象

    它是一个对象,它返回一个新项加载器到填充给定项目。它有以下类:

    class scrapy.loader.ItemLoader([item, selector, response, ]**kwargs)
    

    Input and Output processors

    An Item Loader contains one input processor and one output processor for each (item) field. The input processor processes the extracted data as soon as it’s received (through the add_xpath(), add_css() or add_value() methods) and the result of the input processor is collected and kept inside the ItemLoader. After collecting all data, the ItemLoader.load_item() method is called to populate and get the populated Item object. That’s when the output processor is called with the data previously collected (and processed using the input processor). The result of the output processor is the final value that gets assigned to the item.

    Item Loader 为每个 Item Field 单独提供了一个 Input processor 和一个 Output processor

    Input processor 一旦它通过 add_xpath()add_css()add_value() 方法收到提取到的数据便会执行,执行以后所得到的数据将仍然保存在 ItemLoader 实例中;当数据收集完成以后,ItemLoader 通过 load_item() 方法来进行填充并返回已填充的 Item 实例。

    即input_processor是在收集数据的过程中所做的处理,output_processor是数据yield之后进行的处理,通过下面这个例子会更加理解:

    #type字段取出来时是'type': ['2室2厅', '中楼层/共6层']
    
    #定义一个在第一个元素后面加a的函数
    def adda(value):
        return value[0]+'a'
    
    type = scrapy.Field(output_processor = Compose(adda))
    >>>'type': '2室2厅a'
    
    type = scrapy.Field(input_processor = Compose(adda))
    >>>'type': ['2室2厅a', '中楼层/共6层a']
    #如果使用MapCompose的话,两个结果会一样,这也是Compose和MapCompose的区别
    

    当指定了取列表的第一个元素后,有些信息想保留整个列表便可以使用name_out,Identity()是取自身的函数。

    class TeItem(ItemLoader):
        default_out_processor = TakeFirst()
        name_out = Identity()
    

    也可以在基于scrapy.Item的item中定义一些规则:

    # 自定义itemloader,继承ItemLoader
    class Scrapy1Item(scrapy.Item):
        name = scrapy.Field(output_processor=Identity())
    
    优先级

    scrapy提供了很多种方式去自定义输入输出的内容,具有一定的优先级,优先级最高的是name_out这种,其次是在scrapy.Field()中定义的output_processor和input_processor,最后是default_out_processor = TakeFirst()这种。

    常见的内置处理器

    1、Identity

    不对数据进行处理,直接返回原来的数据

    2、TakeFirst

    返回第一个非空值,常用于单值字段的输出处理

    3、Join

    相当于把列表中的元素拼接起来

    4、MapCompose把几个方法组合起来


    参考:
    ItemLoader讲解 https://www.cnblogs.com/python-dd/p/9541721.html
    Itemloader数据清洗–input_processor和output_processor比较 http://www.cnblogs.com/python-dd/p/9550433.html
    scrapy官方文档 https://docs.scrapy.org/en/latest/topics/loaders.html#input-and-output-processors
    Scrapy项目加载器(Item Loader) https://www.yiibai.com/scrapy/scrapy_item_loaders.html
    scrapy_ItemLoader https://www.cnblogs.com/2bjiujiu/p/7237443.html
    Scrapy官方文档阅读——Itemloader https://blog.csdn.net/dhaiuda/article/details/81501350
    ItemLoader的简单使用 http://www.cnblogs.com/ruoniao/p/6925839.html

  • 相关阅读:
    Spring中Model,ModelMap以及ModelAndView之间的区别
    ssm框架中Controller层的junit测试_我改
    Controller、Service、Dao进行Junit单元
    ssm controller层 junit单元测试
    spring常用注解
    Spring MVC测试框架
    ssm框架junit简单测试_我写
    spring注入时报错::No qualifying bean of type 'xxx.xxMapper'
    Eclipse 报 "The builder launch configuration could not be found" 错误
    IIS负载均衡-Application Request Route详解第一篇: ARR介绍(转载)
  • 原文地址:https://www.cnblogs.com/onefine/p/10499353.html
Copyright © 2011-2022 走看看