1

我想检查是否在 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 的情况下检查字段是否设置?

4

1 回答 1

1

终于找到了。事实证明,即使我们可以在 python 2.x 上使用 Scrapy,它也希望我们使用 python 3 模式。我应该使用'field' in item而不是item.has_key('field').

于 2018-05-27T01:00:03.743 回答