刮伤:'异常.KeyError'在crawspid中

2024-09-30 01:23:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从以下网站上获取所有相关字段,以便将所有数据加载到电子表格中:

http://yellowpages.com.gh/Home.aspx?

我猜爬行蜘蛛就是我想要的,所以这就是我一直在努力构建的:


^{pr2}$

但是,在命令提示符下运行此命令时,我收到以下错误:

异常.KeyError:'项目不支持字段:catLink'

发生这种错误的最可能原因是什么?它能和我的xpath格式联系起来吗?或者是因为这只蜘蛛共享相同的信息项目.py文件作为项目中的原始蜘蛛?在

我的项目.py代码如下:

# Define here the models for your scraped items
#
# See documentation in:
# http://doc.scrapy.org/en/latest/topics/items.html

from scrapy.item import Item, Field

class YellowghItem(Item):
    # define the fields for your item here like:
    # name = Field()
      catName = Field()
      catLink = Field()
      subcatText = Field()
      subcatLink = Field()
      company = Field()
      more = Field()
      address = Field()
      postAddress = Field()
      city = Field()
      region = Field()
      mobile = Field()
      emailtext = Field()
      emailLink = Field()
      webtext = Field()
      webLink = Field()

      #pass

Tags: the项目pyhttpfieldforyourhere
1条回答
网友
1楼 · 发布于 2024-09-30 01:23:29

这就是为什么你看到了错误。您的item.py文件定义了类YellowghItem。此类具有类成员catLink。在

但是在你的spider中,你没有实例化这个类。相反,您正在实例化一个Item()类。我敢打赌在您的项目中还有另一个名为Item的类,它没有将catLink定义为它的成员。在

在你的蜘蛛上做这些改变:

  • scrapy.item import YellowghItem更改导入
  • 在您的parse方法中,用以下内容实例化此类的对象:

    item = YellowghItem()
    

尝试这些更改,我认为您将能够解决此错误。在

希望这有帮助。在

相关问题 更多 >

    热门问题