python - 无法在scrapy爬虫中导入我自己的模块

标签 python scrapy

我正在使用 Scrapy 编写爬虫。我已经构建了一个爬虫,它运行良好。

现在我想创建自己的模块,但总是收到此错误:

File "D:\Projects\bitbucket\terranoha\crawl1\crawl1\spiders\samplecrawler.py", line 4, in import moduletest

ModuleNotFoundError: No module named 'moduletest'

代码是:

from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
import moduletest

class SamplecrawlerSpider(CrawlSpider):
    # [...]

我正在运行:scrapy crawl --nolog samplecrawler。我在 Windows 10 上。

我的项目结构是:

enter image description here

最佳答案

你可以做几件事:

第一

from crawl1.spiders.moduletest import mythings

根据@elRuLL 的建议

第二

from .moduletest import mythings

这通常是一个糟糕而脆弱的解决方案,但有可能。

第三

你可以把它打包成包然后做。

init.py:

from spiders.moduletest import *
__all__ = [<Put your classes, methods, etc here>]

samplecrawler.py

import moduletest

关于python - 无法在scrapy爬虫中导入我自己的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53871052/

相关文章:

python - Scrapy:从 python 脚本运行蜘蛛并等待它执行结束

python - 谷歌合作实验室: How can i mount two google drive accounts?

python - Django:如何使用 auth_user 表的 is_active?

python - 在 falcon Python 中分隔路由的正确方法是什么?

python - 为什么 cURL 给出正确的响应而 scrapy 没有?

python - 我怎样才能停止一个 scrapy CrawlSpider 并稍后从它停止的地方恢复?

html - "#"像这样使用时的含义 href ="#"

python - Pyinstaller 在文件提取过程中添加启动画面或视觉反馈

python - 打字:定义一个只能是某些字符串的类型?

python - 对于 scrapy/selenium 有没有办法返回到上一页?