为什么我的蜘蛛 运行 时得到 "ModuleNotFoundError"?

Why do I get "ModuleNotFoundError" when I run my spider?

我正在使用 scrapy 1.5.2 和 python 3.

我有一个非常简单的蜘蛛,我创建了一个小管道来转换我的项目的日期字段。

这是我的项目 "entreprises" 的树文件夹:http://prntscr.com/o8axfc

如您在此屏幕截图中所见,我创建了一个文件夹 "pipelines",并在其中添加了 tidyup.py 文件,并在其中添加了以下代码:

from datetime import datetime

class TidyUp(object):
    def process_item(self, item, spider):
        item['startup_date_creation']= map(datetime.isoformat, item['startup_date_creation'])
        return item

您还可以在我的屏幕截图中看到我在项目的 settings.py 中添加了参数:

ITEM_PIPELINES = {'entreprises.pipelines.tidyup.TidyUp': 100}

这是我的蜘蛛使用的代码-digitale2.py:

# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from scrapy.utils.response import open_in_browser


def parse_details(self,response):
    if "item_name" not in response.body:
        open_in_browser(response)
        
    item=response.mega.get('item',None)
    if item:
        return item
    else:
        self.logger.warning("pas d'item reçu pour %s", response.url)
        
        
        

class UsineDigital2Spider(CrawlSpider):
    name = 'usine-digital2'
    allowed_domains = ['website.fr']
    start_urls = ['https://www.website.fr/annuaire-start-up/']

    rules = (
        Rule(LinkExtractor(restrict_xpaths="//*[@rel='next']")),
        Rule(LinkExtractor(restrict_xpaths="//*[@itemprop='url']"),
             callback='parse_item')
    )

    def parse_item(self, response):
        i = {}    
        
        i["startup_name"] = response.xpath("//h1/text()").extract()
        i["startup_date_creation"] = response.xpath("//*[@itemprop='foundingDate']/@content").extract()
        i["startup_website"] = response.xpath ("//*[@id='infoPratiq']//a/@href").extract()
        i["startup_email"] = response.xpath ("//*[@itemprop='email']/text()").extract()
        i["startup_address"] = response.xpath ("//*[@id='infoPratiq']//p/text()").extract()
        i["startup_founders"] = response.xpath ("//*[@itemprop='founders']/p/text()").extract()
        i["startup_market"] = response.xpath ("//*[@id='ficheStartUp']/div[1]/article/div[6]/p").extract()
        i["startup_description"] = response.xpath ("//*[@itemprop='description']/p/text()").extract()
        i["startup_short_description"] = response.xpath ("//*[@itemprop='review']/p").extract()
        
        return i

当我运行命令时:

scrapy crawl usine-digital2 -s CLOSESPIDER_ITEMCOUNT=30

我收到此错误消息:

ModuleNotFoundError: No module named 'entreprises.pipelines.tidyup'; 'entreprises.pipelines' is not a package

这是我的终端登录:

http://prntscr.com/o8azt0

我在我的代码中到处搜索。我没有看到任何错误。此代码来自 "Learn Scrapy" 一书(来自 Dimitrios Kouzis-loukas),我按照其中的说明进行操作。我不明白为什么它不起作用。

您可以在此处找到 scrapy 项目 "entreprises" 的所有源代码:

https://github.com/FormationGrowthHacking/scrapy/tree/master/entreprises

因为我在看书"Learn Scrapy",你很容易猜到我是一个正在开发他的第一个爬虫的新手。非常感谢专家的帮助。

亲切的问候

您的项目中有 pipelines 文件夹 pipelines.py 文件,这是导致问题的原因。

我建议删除该文件夹并将您的管道 class 移至 pipelines.py 文件

删除 pipelines.py 并使用此导入添加 pipelines/__init__.py

# -*- coding: utf-8 -*-
from .tidyup import TidyUp

也在settings.py

ITEM_PIPELINES = {'entreprises.pipelines.TidyUp': 100}