Python / Scrapy导入错误:无法导入名称

时间:2014-09-27 02:14:47

标签: python import scrapy

我是Python / Scrapy的新手。使用python2.7

我的错误:

line 5 in module  from scrapy.item import wikispiderItem
Import Error: Can not import name wikispiderItem

文件夹/文件结构

C:\wikispider\wikispider

    -_init_.py
    -items.py
    -pipelines.py
    -settings.py
          -_init_.py
          -wikiSpider.py

items.py代码

来自scrapy.items的

导入项目,字段,链接

class wikispiderItem(scrapy.items):
    # define the fields for your item here like:
    # name = scrapy.Field()
    hxs = Field()
    heading = Field()
    link = Field()

wikispider.py代码

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
from scrapy.http import Request
from scrapy.item import wikispiderItem

class wikiSpider(CrawlSpider):
    name = "wikiSpider"
    allowed_domains = ['wikipedia.org']
    start_urls = ['http://en.wikipedia.org/wiki/Mathematics']

    rules = (
        Rule(SgmlLinkExtractor(restrict_xpaths=('//div[@class="mw-body"]//a/@href'))),
        Rule(SgmlLinkExtractor( allow=("http://en.wikipedia.org/wiki/",)),callback='parse_item'),
        )
    def parse_item(self, response):
        hxs =HtmlXPathSelector(response)
        print hxs.select('//h1[@class="firstHeading"]/span/text()').extract()
        headng = hxs.select('//h1[@class="firstHeading"]/span/text()').extract()

2 个答案:

答案 0 :(得分:1)

from wikispider.items import wikispiderItem

您的items.py应该是:

import scrapy


class wikispiderItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    hxs = scrapy.Field()
    heading = scrapy.Field()
    link = scrapy.Field()

答案 1 :(得分:1)

from scrapy.item import wikispiderItem

wikispider.py中的上一行应该是......

from wikispider.items import wikispiderItem