scrapy 项目加载器返回列表不是单个值 [英] scrapy item loader return list not single value

查看:44
本文介绍了scrapy 项目加载器返回列表不是单个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是scrapy 0.20.

I am using scrapy 0.20.

我想使用项目加载器

这是我的代码:

l = XPathItemLoader(item=MyItemClass(), response=response)
        l.add_value('url', response.url)
        l.add_xpath('title',"my xpath")
        l.add_xpath('developer', "my xpath")
return l.load_item()

我在json文件中得到了结果.url 是一个列表.title 是一个列表.developer 是一个列表.

I got the result in the json file. the url is a list. The title is a list. The developer is a list.

如何提取单个值而不是列表?

How to extract single value instead of the list?

我应该为此制作一个项目管道吗?我希望有更快的方法

推荐答案

您需要设置一个 输入或输出处理器.TakeFirst 在你的情况下会很完美.

You need to set an Input or Output processor. TakeFirst would work perfectly in your case.

您可以在多个地方定义它,例如在 Item 定义中:

There are multiple places where you can define it, e.g. in the Item definition:

from scrapy.item import Item, Field
from scrapy.loader.processors import TakeFirst

class MyItem(Item):
    url = Field(output_processor=TakeFirst())
    title = Field(output_processor=TakeFirst())
    developer = Field(output_processor=TakeFirst())

或者,设置一个default_output_processorXpathItemLoader() 实例上:

Or, set a default_output_processor on a XpathItemLoader() instance:

l.default_output_processor = TakeFirst()

这篇关于scrapy 项目加载器返回列表不是单个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆