Scrapy: Default values for items & fields. What is the best implementation?

末鹿安然 提交于 2020-01-21 11:09:05

问题


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.

This doesn't work

category = Field(default='null')

So my question is: what is a good way to initialize fields with a default value?

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


回答1:


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


来源:https://stackoverflow.com/questions/15694501/scrapy-default-values-for-items-fields-what-is-the-best-implementation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!