Joey Chestnut Joey Chestnut - 6 months ago 20
Python Question

Python/Scrapy Import Error: Can not import name

Im new to Python/Scrapy. using python2.7

My error:

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


Folder/File Structure

C:\wikispider\wikispider

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


items.py code

from scrapy.items import Item, Field, Link

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


wikispider.py code

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()

Answer

from wikispider.items import wikispiderItem

Your items.py should be:

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()