Yuting
Yuting

Reputation: 131

Scrapy Item Not Defined

I'm writing a crawler to get some pages from Yelp. I define the Yelp Item like this:

yelpItem.py:

import scrapy

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

and in the spider folder, I use YelpItem in the parse function.

def parse(self, response):
    hxs = HtmlXPathSelector(response)
    sites = hxs.select('//h3/span/a[contains(@class, "biz-name")]')
    items = []
    for site in sites:
        item = YelpItem()

When running it, it says:

NameError: global name 'YelpItem' is not defined

I had searched several webpages, and tried to add codes like:

from hw1.items import YelpItem

(hw1 is my project name),but it is not helping. This will leads to an error like: No module named items

Can anyone help me to figure out how to deal with this? Thanks!

Upvotes: 2

Views: 3144

Answers (1)

GHajba
GHajba

Reputation: 3691

Use

from hw1.yelpItem import YelpItem

Because when you try from hw1.items you are referencing to the items.py file but your YelpItem is in the yelpItem.py file you have to update the import path too.

You can read about the background why it is so here.

Upvotes: 2

Related Questions