为什么在运行spider时会出现“ModuleNotFoundError”?

2024-04-25 05:18:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在python3中使用scrapy1.5.2。你知道吗

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

这是我的“Enterprises”项目的树文件夹: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}

以下是我的蜘蛛usine-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

我在密码里到处找。我没有看到任何错误。这段代码是从书“学习刮”(从迪米特里奥斯库齐斯卢卡斯)在那里我遵循指示。我不明白为什么它不起作用。你知道吗

你可以在这里找到所有“企业”项目的源代码:

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

当我在读《学刮胡刀》这本书时,你很容易猜到我是一个新手,想开发他的第一个刮胡刀。如果能得到专家的帮助,我将不胜感激。你知道吗

谨致问候


Tags: 项目textfromimportselfcomresponseextract
1条回答
网友
1楼 · 发布于 2024-04-25 05:18:50

您的项目中有pipelines文件夹pipelines.py文件,这导致了问题。你知道吗

enter image description here

我建议删除文件夹并将管道类移到pipelines.py文件中

通过此导入删除pipelines.py并添加pipelines/__init__.py

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

同样在settings.py

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

相关问题 更多 >