python - Scrapy ItemLoader如何选择返回结果list中的第二个以及以后的值?
PHP中文网
PHP中文网 2017-04-17 17:43:35
0
2
316

在items.py中加入:

from scrapy.loader.processors import TakeFirst
    ......
    # 修改为以下内容
    followees = Field(output_processor=TakeFirst())

其他的不用改变,在spiders中的代码不需要改变

亟待解决:问题是第二个怎么选以及之后的几个?

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(2)
大家讲道理

Try adding a class to scrapy.loader.processors.
I found the TakeFirst class and then added

after it
#第二个数据
class TakeSecond(object):

    def __call__(self, values):
        for i in range(len(values)):
            if i == 1 :
                if values[i] is not None and values[i] != '':
                    return values[i]

Then import TakeSecond in item.py and use

    name = scrapy.Field(
        input_processor=MapCompose(remove_tags),
        output_processor=Join(),
    )
    price = scrapy.Field(
        input_processor=MapCompose(remove_tags, filter_price),
        output_processor=TakeSecond(),
    )

Then run the result

>>> from scrapy.loader import ItemLoader
>>> from tutorial.items import Product
>>> il = ItemLoader(item=Product())
>>> il.add_value("price", ['333','444'])
>>> il.load_item()
{'price': u'444'}
迷茫

from scrapy.loader.processors import TakeFirst

......
followees = Field(output_processor=Compose(lambda x: x[1:]))
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template