无法在scrapy蜘蛛中导入items.py

时间:2019-07-09 18:27:35

标签: python-3.x scrapy

由于找不到我的项目模块时出错,我无法使用shell命令“ scrapy crawl kbb”运行我的蜘蛛。

我的文件夹路径遵循标准的scrapy方向。

# -*- coding: utf-8 -*-
import scrapy
from scrapy.loader import ItemLoader
from kbb.items import KelleyItem


class KbbSpider(scrapy.Spider):
    name = 'kbb'
    allowed_domains = ['kbb.com']
    start_urls = ['https://www.kbb.com/cars-for-sale/cars/?distance=75']

    def parse(self, response):
        l = ItemLoader(item=Product(), response=response)
        l.xpath('Title','//div[@class="listings-container-redesign"]/div/div/a/text()').extract()
        l.xpath('Price','//div[@class="listings-container-redesign"]/div/div/div/div/span/text()').extract()
        return l.load_item()

items.py:

# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html

import scrapy


class KelleyItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    title = scrapy.Field()
    price = scrapy.Field()

通过外壳命令“ scrapy crawl kbb”运行此命令时,出现以下错误:“ ModuleNotFoundError:没有名为kbb的模块”

2 个答案:

答案 0 :(得分:0)

from items import KelleyItem

尝试这个。

答案 1 :(得分:0)

如果您的项目使用标准的scrapy文件夹结构,则可以使用以下方法:

from ..items import KelleyItem

See relative imports in Python