【发布时间】:2016-12-23 01:12:03
【问题描述】:
我有兴趣保持对 scrapy 项目中字段名称顺序的引用。这个存储在哪里?
>>> dir(item)
Out[7]:
['_MutableMapping__marker',
'__abstractmethods__',
'__class__',
'__contains__',
'__delattr__',
'__delitem__',
'__dict__',
'__doc__',
'__eq__',
'__format__',
'__getattr__',
'__getattribute__',
'__getitem__',
'__hash__',
'__init__',
'__iter__',
'__len__',
'__metaclass__',
'__module__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__setitem__',
'__sizeof__',
'__slots__',
'__str__',
'__subclasshook__',
'__weakref__',
'_abc_cache',
'_abc_negative_cache',
'_abc_negative_cache_version',
'_abc_registry',
'_class',
'_values',
'clear',
'copy',
'fields',
'get',
'items',
'iteritems',
'iterkeys',
'itervalues',
'keys',
'pop',
'popitem',
'setdefault',
'update',
'values']
我尝试了 item.keys(),但它返回了一个无序的字典
【问题讨论】:
-
我认为
Scrapy不会保持项目中字段的顺序(但我不确定)。 -
实际上(截至今天),如果您在
process_item(self, item, spider)内部(在您的pipelines.py中),那么使用item.keys()保留在items.py文件中找到的顺序。但是,如果您尝试通过从items.py导入类来获取项目keys,则使用self.ikeys = YourItem.fields.keys()时找到的顺序将按字母 顺序排列。下面的答案没有解决这个问题。