【问题标题】:Scrapy ImportError: cannot import name "______Item"Scrapy ImportError:无法导入名称“______Item”
【发布时间】:2015-03-25 10:12:34
【问题描述】:

我是 python 2.7 和 Scrapy 的新手,从命令行运行“scrapy crawl prop$”时收到以下错误。我认为这是一个简单的修复,因此,非常感谢任何帮助!

错误信息:

  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 

【问题讨论】:

  • 类名是 PropubItem 不是 propubItem

标签: python python-2.7 scrapy


【解决方案1】:

这只是一个错字。您的项目类名称​​以大写字母开头

替换:

from propub.items import propubItem

与:

from propub.items import PropubItem

【讨论】:

猜你喜欢
  • 2016-06-09
  • 2014-11-22
  • 1970-01-01
  • 2016-02-28
  • 2016-03-31
  • 2014-10-10
  • 2014-09-20
  • 2014-08-28
  • 2014-06-10
相关资源
最近更新 更多