【问题标题】:Cannot import items.py in scrapy spider无法在scrapy spider中导入items.py
【发布时间】:2019-07-09 18:27:35
【问题描述】:

由于在查找我的项目模块时出错,我无法使用 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()

当通过 shell 命令“scrapy crawl kbb”运行它时,我收到以下错误:“ModuleNotFoundError: No module named kbb”

【问题讨论】:

    标签: python-3.x scrapy


    【解决方案1】:

    如果你的项目使用标准的scrapy文件夹结构,你可以使用这个:

    from ..items import KelleyItem
    

    See relative imports in Python

    【讨论】:

      【解决方案2】:
      from items import KelleyItem
      

      试试这个。

      【讨论】:

      • 谢谢。刚试了下,报错:ModuleNotFoundError: No module named 'items'
      • 发布项目的文件夹结构。
      猜你喜欢
      • 2022-11-17
      • 1970-01-01
      • 2021-09-18
      • 1970-01-01
      • 2018-10-02
      • 1970-01-01
      • 1970-01-01
      • 2020-08-17
      • 1970-01-01
      相关资源
      最近更新 更多