scrapy:CrawlSpider中的'exceptions.KeyError'

时间:2014-04-20 16:16:10

标签: python regex xpath scrapy

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

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

我猜测CrawlSpider就是我想要的,所以这就是我一直在努力构建的:


from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import Selector
from scrapy.item import Item
class YellowGH2Spider(CrawlSpider):
    name = "yellowGH2"
    allowed_domains = ["yellowpages.com.gh"]
    start_urls = ["http://yellowpages.com.gh/Home.aspx"]
    rules = (
        Rule(SgmlLinkExtractor(allow=(r'http://yellowpages.com.gh/Home.aspx?mcaid=\d+#tabs-2', ))),
        Rule(SgmlLinkExtractor(allow=(r'http://yellowpages.com.gh/(Home|Search-Results).aspx?mcaid=[0-9&eca1id=]+(&lcaid=)?\d+#tabs-2', )), callback='parse_item'),
        Rule(SgmlLinkExtractor(allow=(r'http://yellowpages.com.gh/Company-Details/[a-zA-Z0-9-]+.aspx?returnurl=/Search-Results.aspx', )), callback='parse_item'),
        )
    def parse(self, response):
        #hxs = HtmlXPathSelector(response)
        #filename = response.url.split("/")[-2]
        #open(filename, 'wb').write(response.body)

        sel = Selector(response)
        item = Item()
        #item['catName']=sel.xpath('//div[@class="oneDirCat"]/h3/a/text()').extract()
        item['catLink']=sel.xpath('//div[@class="oneDirCat"]/h3/a/@href').extract()
        item['subcatText']=sel.xpath('//ul/li/a/@href').extract()
        item['subcatLink']=sel.xpath('//div[@class="oneDirCat"]/h3/a/text()').extract()
        item['company']=sel.xpath('//label/text()').extract()
        item['more']=sel.xpath('//td[@valign="bottom"]/a/@href').extract()
        item['address']=sel.xpath('//td[2]/text()').extract()
        item['postAddress']=sel.xpath('//td[4]/text()').extract()
        item['city']=sel.xpath('//td[6]/text()').extract()
        item['region']=sel.xpath('//td[8]/text()').extract()
        item['mobile']=sel.xpath('//td[12]/text()').extract()
        item['emailtext']=sel.xpath('//td[16]/a/text()').extract()
        item['emailLink']=sel.xpath('//td[16]/a/@href').extract()
        item['webtext']=sel.xpath('//td[18]/a/text()').extract()
        item['webLink']=sel.xpath('//td[18]/a/@href').extract()
        return item


            #print catName, catLink, subcatText, subcatLink, company, more,
            #address, postAddress, city, region, mobile, emailtext, emailLink,
            #webtext, webLink

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

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

出现错误的最可能原因是什么?它可以与我的XPath格式连接吗?或者它可能与这个蜘蛛共享与项目中原始蜘蛛相同的items.py文件的事实有关吗?

我的items.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

1 个答案:

答案 0 :(得分:4)

所以这就是你看到错误的原因。您的item.py文件已定义了类YellowghItem。该类具有类成员catLink

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

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

  • scrapy.item import YellowghItem
  • 更改导入
  • parse方法中,使用以下函数实例化此类的对象

    item = YellowghItem()
    

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

希望这有帮助。