【问题标题】:Scrapy: AttributeError: 'list' object has no attribute 'iteritems'Scrapy:AttributeError:'list'对象没有属性'iteritems'
【发布时间】:2016-09-23 09:33:54
【问题描述】:

这是我关于堆栈溢出的第一个问题。最近想用linked-in-scraper,所以我下载并指令“scrapy crawllinkedin.com”,得到以下错误信息。供您参考,我使用 anaconda 2.3.0 和 python 2.7.11。所有相关的包,包括scrapy和六个,都是在执行程序之前通过pip更新的。

Traceback (most recent call last):
  File "/Users/byeongsuyu/anaconda/bin/scrapy", line 11, in <module>
    sys.exit(execute())
File "/Users/byeongsuyu/anaconda/lib/python2.7/site-packages/scrapy/cmdline.py", line 108, in execute
settings = get_project_settings()
File "/Users/byeongsuyu/anaconda/lib/python2.7/site-packages/scrapy/utils/project.py", line 60, in get_project_settings
settings.setmodule(settings_module_path, priority='project')
File "/Users/byeongsuyu/anaconda/lib/python2.7/site-packages/scrapy/settings/__init__.py", line 285, in setmodule
self.set(key, getattr(module, key), priority)
  File "/Users/byeongsuyu/anaconda/lib/python2.7/site-packages/scrapy/settings/__init__.py", line 260, in set
self.attributes[name].set(value, priority)
  File "/Users/byeongsuyu/anaconda/lib/python2.7/site-packages/scrapy/settings/__init__.py", line 55, in set
value = BaseSettings(value, priority=priority)
  File "/Users/byeongsuyu/anaconda/lib/python2.7/site-packages/scrapy/settings/__init__.py", line 91, in __init__
self.update(values, priority)
  File "/Users/byeongsuyu/anaconda/lib/python2.7/site-packages/scrapy/settings/__init__.py", line 317, in update
for name, value in six.iteritems(values):
  File "/Users/byeongsuyu/anaconda/lib/python2.7/site-packages/six.py", line 599, in iteritems
return d.iteritems(**kw)

AttributeError: 'list' object has no attribute 'iteritems'

我知道这个错误源于 d 不是字典类型而是列表类型。而且由于错误来自scrapy上的代码,可能是scrapy包或六包的问题。如何尝试修复此错误?

编辑:这是来自 scrapy.cfg 的代码

  # Automatically created by: scrapy start project
  #
  # For more information about the [deploy] section see:
  # http://doc.scrapy.org/topics/scrapyd.html
  [settings]  
  default = linkedIn.settings

   [deploy]
   #url = http://localhost:6800/
   project = linkedIn

【问题讨论】:

  • 你有 Scrapy 的配置文件吗?看起来它希望读取字典,但找到了一个列表。
  • @ValentinLorentz 是的,我添加了上面的代码。但我认为它没有关于这个问题的额外信息。编写此代码的程序员说它在带有 python 2.7.6 的 Ubuntu 上运行良好。

标签: python scrapy-spider six


【解决方案1】:

这是由链接的刮板的settings引起的:

ITEM_PIPELINES = ['linkedIn.pipelines.LinkedinPipeline']

但是,ITEM_PIPELINES 应该是一个字典,according to the doc

要激活 Item Pipeline 组件,您必须将其类添加到 ITEM_PIPELINES 设置中,如下例所示:

ITEM_PIPELINES = {
    'myproject.pipelines.PricePipeline': 300,
    'myproject.pipelines.JsonWriterPipeline': 800,
}

您在此设置中分配给类的整数值决定了它们的运行顺序:项目从低值到高值的类。通常将这些数字定义在 0-1000 范围内。

根据this question,它曾经是一个列表,这就解释了为什么这个爬虫使用一个列表。 所以你要么要求你的爬虫开发者更新他们的代码,要么自己设置ITEM_PIPELINES

【讨论】:

    【解决方案2】:

    简短的回答是 ITEM_PIPELINES 应该是一个字典,而不是一个列表,其中键作为管道类,值是一个整数,确定它们运行的​​顺序:项目从低值到高值类。通常将这些数字定义在 0-1000 范围内。正如@valentin Lorentz 所解释的那样

    【讨论】:

      猜你喜欢
      • 2012-10-30
      • 2015-08-05
      • 1970-01-01
      • 2016-08-25
      • 1970-01-01
      • 1970-01-01
      • 2020-08-09
      • 2019-03-19
      • 2021-01-04
      相关资源
      最近更新 更多