Scrapy BaseSpider:它是如何工作的?

时间:2009-11-27 00:15:38

标签: python web-crawler scrapy

这是Scrapy教程中的BaseSpider示例:

from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector

from dmoz.items import DmozItem

class DmozSpider(BaseSpider):
   domain_name = "dmoz.org"
   start_urls = [
       "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/",
       "http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/"
   ]

   def parse(self, response):
       hxs = HtmlXPathSelector(response)
       sites = hxs.select('//ul[2]/li')
       items = []
       for site in sites:
           item = DmozItem()
           item['title'] = site.select('a/text()').extract()
           item['link'] = site.select('a/@href').extract()
           item['desc'] = site.select('text()').extract()
           items.append(item)
       return items

SPIDER = DmozSpider()

我通过对项目的更改复制了它:

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
from scrapy.item import Item
from firm.items import FirmItem

class Spider1(CrawlSpider):
    domain_name = 'wc2'
    start_urls = ['http://www.whitecase.com/Attorneys/List.aspx?LastName=A']

    def parse(self, response):
        hxs = HtmlXPathSelector(response)
        sites = hxs.select('//td[@class="altRow"][1]/a/@href').re('/.a\w+')
        items = []
        for site in sites:
            item = FirmItem
            item['school'] = hxs.select('//td[@class="mainColumnTDa"]').re('(JD)(.*?)(\d+)')
            items.append(item)
        return items

SPIDER = Spider1()    

我收到了错误

[wc2] ERROR: Spider exception caught while processing   
<http://www.whitecase.com/Attorneys/List.aspx?LastName=A> (referer: <None>): 
[Failure instance: Traceback: <type 'exceptions.TypeError'>: 
'ItemMeta' object does not support item assignment

如果专家在这里查看代码并给我一个关于我哪里出错的线索,我将不胜感激。

谢谢

1 个答案:

答案 0 :(得分:16)

可能你的意思是item = FirmItem()而不是item = FirmItem