我想检查 Scrapy 项目中是否设置了字段。但我在该项目上使用 has_key
时,出现此错误:
Traceback (most recent call last):
File "d:\python27\lib\site-packages\twisted\internet\defer.py", line 653, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "D:\Kerja\HIT\Python Projects\<project_name>\<project_name>\<project_name>\pipelines.py", line 16, in process_item
self.listing_process.process_new_or_update(item)
File "D:\Kerja\HIT\Python Projects\<project_name>\<project_name>\<project_name>\processor.py", line 290, in process_new_or_update
listing = self.listing_check.normalize_for_process(listing)
File "D:\Kerja\HIT\Python Projects\<project_name>\<project_name>\<project_name>\processor.py", line 213, in normalize_for_process
if listing.has_key('description'):
File "d:\python27\lib\site-packages\scrapy\item.py", line 74, in __getattr__
raise AttributeError(name)
AttributeError: has_key
如何在不使用 has_key 的情况下检查字段是否已设置?
最佳答案
终于找到了。事实证明,尽管我们可以在 python 2.x 上使用 Scrapy,但它希望我们使用 python 3 模式。我应该使用 'field' in item
而不是 item.has_key('field')
。
关于python - Scrapy 中项目的 has_key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50548180/