Scrapy ImportError: No module named Item

2019-07-27 12:36发布

I know that this question was already widely discussed, but I didn't find an answer. I'm getting error ImportError: No module named items. I've created a new project with $ scrapy startproject pluto and I have no equal names (in names of project, classes etc), to avoid problem with naming.

pluto_spider.py :

import scrapy
from items import PlutoItem

class PlutoSpider(scrapy.Spider):
  name = "plutoProj"

  allowed_domains = ['successories.com']
  start_urls = [
        'http://www.successories.com/iquote/category/39/inspirational-quotes/4',
        'http://www.successories.com/iquote/category/39/inspirational-quotes/6',
        ]

def parse(self,response):

  items = []

  for quote in response.css('div.quotebox'):
    item = PlutoItem()
    item['author'] =  quote.css('span.author a::text').extract_first()
    item['quote'] = quote.css('div.quote a::text').extract_first()
    items.append(item)
    return items

item.py :

import scrapy

class PlutoItem(scrapy.Item):
  author = scrapy.Field()
  quote = scrapy.Field()

This my folder's hierarchy:

/pluto
/pluto/scrapy.cfg
/pluto/pluto/__init__.pyc
/pluto/pluto/__init__.py
/pluto/pluto/items.py
/pluto/pluto/pipelines.py
/pluto/pluto/settings.py
/pluto/pluto/settings.pyc
/pluto/pluto/spiders/__init__.py
/pluto/pluto/spiders/__init__.pyc
/pluto/pluto/spiders/pluto_spider.py
/pluto/pluto/spiders/pluto_spider.pyc

标签: python scrapy
2条回答
我命由我不由天
2楼-- · 2019-07-27 12:45

It looks like your items.py and pluto_spider.py are at different levels. You should make your import either from pluto import items or a relative import import ..items per PEP 328 to import the module.

If you want the class from pluto.items import PlutoItem

查看更多
劳资没心,怎么记你
3楼-- · 2019-07-27 13:02

First of all, make sure to execute the Scrapy command from inside the top level directory of your project.

Or you may also try changing your import to:

from pluto.items import PlutoItem
查看更多
登录 后发表回答