2015-03-25 220 views
1

我是python 2.7的新手& Scrapy,在命令行運行「scrapy crawl prop $」時收到以下錯誤消息。我認爲這是一個簡單的修復,因此,任何援助非常感謝!Scrapy ImportError:無法導入名稱「______Item」

錯誤消息:

File"C:\Anaconda2\propub\propub\spiders\propub_spider.py", line 4, in <module> 
    from propub.items import propubItem 
ImportError: cannot import name propubItem 

items.py

import scrapy 
from scrapy.item import Item, Field 

class PropubItem(scrapy.Item): 
    payee = scrapy.Field() 
    link = scrapy.Field() 
    city = scrapy.Field() 
    state = scrapy.Field() 
    company = scrapy.Field() 
    amount = scrapy.Field() 
    pass 

propub_spiders.py

import scrapy 
from scrapy.contrib.spiders import CrawlSpider, Rule 
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor 
from propub.items import propubItem 

class propubSpider(CrawlSpider): 
    name = 'prop$' 
    allowed_domains = ['https://projects.org'] 
    start_urls = [ 
     'https://projects/search?state%5Bid%5D=33', 
     'https://projects/search?page=2&state%5Bid%5D=33', 
     'https://projects/search?page=3&state%5Bid%5D=33'] 

    rules = (Rule(SgmlLinkExtractor(allow=('\\search?page=\\d')), 'parse_start_url', follow=True),) 

    def parse(self, response): 
     for sel in response.xpath('//*[@id="payments_list"]/tbody'): 
      item = propubItem() 
      item['payee'] = sel.xpath('tr[1]/td[1]/a[2]/text()').extract() 
      item['link'] = sel.xpath('tr[1]/td[1]/a[1]/@href').extract() 
      item['city'] = sel.xpath('tr[1]/td[2]/text()').extract() 
      item['state'] = sel.xpath('tr[1]/td[3]/text()').extract() 
      item['company'] = sel.xpath('tr[1]/td[4]').extract() 
      item['amount'] = sel.xpath('tr[1]/td[7]/span/text()').extract() 
      yield item 
+0

類名PropubItem不propubItem – 2015-03-25 06:24:17

回答

1

這僅僅是一個錯字。您的物品類名稱以大寫字母開頭。

替換:

from propub.items import propubItem 

有:

from propub.items import PropubItem 
+0

嗨,你會介意給這個幫助? http://stackoverflow.com/questions/31493417/scrapy-import-module-items-error – yukclam9 2015-07-19 05:32:49

相關問題