Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to import items in scrapy

I have a very basic spider, following the instructions in the getting started guide, but for some reason, trying to import my items into my spider returns an error. Spider and items code is shown below:

from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector

from myProject.items import item

class MyProject(BaseSpider):
    name = "spider"
    allowed_domains = ["website.com"]
    start_urls = [
        "website.com/start"
    ]

    def parse(self, response):
        print response.body

from scrapy.item import Item, Field

class ProjectItem(Item):
    title = Field()

When I run this code scrapy either can't find my spider, or can't import my items file. What's going on here? This should be a really example to run right?

like image 494
Slater Victoroff Avatar asked Dec 05 '22 11:12

Slater Victoroff


2 Answers

I also had this several times while working with scrapy. You could add at the beginning of your Python modules this line:

from __future__ import absolute_import

More info here:

  • http://www.python.org/dev/peps/pep-0328/#rationale-for-absolute-imports
  • http://pythonquirks.blogspot.ru/2010/07/absolutely-relative-import.html
like image 94
warvariuc Avatar answered Dec 20 '22 21:12

warvariuc


you are importing a field ,you must import a class from items.py like from myproject.items import class_name.

like image 24
Manoj Sahu Avatar answered Dec 20 '22 21:12

Manoj Sahu