Scrapy:items & 的默认值领域.什么是最好的实现? [英] Scrapy: Default values for items & fields. What is the best implementation?
问题描述
据我从文档和网络上的各种讨论中了解到,已删除了将默认值添加到 scrapy 项目中的字段的功能.
As far as I could find out from the documentation and various discussions on the net, the ability to add default values to fields in a scrapy item has been removed.
这不起作用
category = Field(default='null')
所以我的问题是:用默认值初始化字段的好方法是什么?
So my question is: what is a good way to initialize fields with a default value?
我已经尝试按照此处的建议将其实现为项目管道,但没有成功.https://groups.google.com/forum/?fromgroups=#!topic/scrapy-users/-v1p5W41VDQ
I already tried to implement it as a item pipeline as suggested here, without any success. https://groups.google.com/forum/?fromgroups=#!topic/scrapy-users/-v1p5W41VDQ
推荐答案
找出问题所在.管道正在工作(代码如下供其他人参考).我的问题是,我将值附加到一个字段.我希望默认方法适用于这些列表值之一......选择了一种不同的方式并且它可以工作.我现在正在使用自定义的 setDefault 处理器方法实现它.
figured out what the problem was. the pipeline is working (code follows for other people's reference). my problem was, that I am appending values to a field. and I wanted the default method work on one of these listvalues... chose a different way and it works. I am now implementing it with a custom setDefault processor method.
class DefaultItemPipeline(object):
def process_item(self, item, spider):
item.setdefault('amz_VendorsShippingDurationFrom', 'default')
item.setdefault('amz_VendorsShippingDurationTo', 'default')
# ...
return item
这篇关于Scrapy:items & 的默认值领域.什么是最好的实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!