has_key for Scrapy中的项目

时间:2018-05-27 00:52:41

标签: python python-2.7 web-scraping scrapy scrapy-item

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

1 个答案:

答案 0 :(得分:1)

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