我想检查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 - has_key用于Scrapy中的项目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50548180/

10-12 00:23
查看更多