NameError:未定义名称“DmozItem”

时间:2017-04-21 11:20:15

标签: python web-scraping scrapy

仍在掌握Scapy并一直关注this教程。但是当我得到以下内容时遇到一些麻烦:NameError: name 'DmozItem' is not defined当我运行时:

import scrapy
from scrapy import Item, Field

class QuotesItems(scrapy.Item):
    area_name = scrapy.Field()
    room_type = scrapy.Field()
    period = scrapy.Field()
    duration_weekly = scrapy.Field()
    guide_total = scrapy.Field()
    amenities = scrapy.Field()


class QuotesSpider(scrapy.Spider):
    name = "not_quotes"
    start_urls = [
        'http://www.unitestudents.com/',
            ]

    # Step 1
    def parse(self, response):
        for city in response.xpath('//select[@id="frm_homeSelect_city"]/option[not(contains(text(),"Select your city"))]/text()').extract(): # Select all cities listed in the select (exclude the "Select your city" option)
            yield scrapy.Request(response.urljoin("/"+city), callback=self.parse_citypage)

    # Step 2
    def parse_citypage(self, response):
        for url in response.xpath('//div[@class="property-header"]/h3/span/a/@href').extract(): #Select for each property the url
            yield scrapy.Request(response.urljoin(url), callback=self.parse_unitpage)


    # Step 3
    def parse_unitpage(self, response):
        for final in response.xpath('//div/div/div[@class="content__btn"]/a/@href').extract(): #Select final page for data scrape
            yield scrapy.Request(response.urljoin(final), callback=self.parse_final)

    #Step 4 
    def parse(self, response):
        for sel in response.xpath('//html/body/div'):
            item = DmozItem()
            item['area_name'] = sel.xpath('//div/ul/li/a/span/text()').extract()
            item['room_type'] = sel.xpath('//div/div/div/h1/span/text()').extract()
            item['period'] = sel.xpath('/html/body/div/div/section/div/form/h4/span/text()').extract()
            item['duration_weekly'] = sel.xpath('//html/body/div/div/section/div/form/div/div/em/text()').extract()
            item['guide_total'] = sel.xpath('//html/body/div/div/section/div/form/div/div/p/text()').extract()
            item['amenities'] = sel.xpath('//div/div/div/ul/li/p/text()').extract()
            yield item

我已将items.py文件设置为:

class DmozItem(Item):
    area_name = Field()
    room_type = Field()
    period = Field()
    duration_weekly = Field()
    guide_total = Field()
    amenities = Field()
    pass

我不确定这个问题出在哪里?

1 个答案:

答案 0 :(得分:1)

您应该导入 DmozItem

from YourFolderName.items import DmozItem