python - 无法在scrapy爬虫中导入我自己的模块
问题描述
我正在使用 Scrapy 编写爬虫。我已经建立了一个爬虫,它工作得很好。
现在我想创建自己的模块,但我总是收到这个错误:
文件“D:\Projects\bitbucket\terranoha\crawl1\crawl1\spiders\samplecrawler.py”,第 4 行,在导入模块测试中
ModuleNotFoundError:没有名为“moduletest”的模块
代码是:
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
import moduletest
class SamplecrawlerSpider(CrawlSpider):
# [...]
我在跑: scrapy crawl --nolog samplecrawler
。我在 Windows 10 上。
我的项目结构是:
解决方案
你可以做几件事:
第一的
from crawl1.spiders.moduletest import mythings
正如@elRuLL 所建议的那样
第二
from .moduletest import mythings
这通常是一个糟糕而脆弱的解决方案,但可能。
第三
您可以将其打包为包并执行。
初始化.py:
from spiders.moduletest import *
__all__ = [<Put your classes, methods, etc here>]
样本爬虫.py
import moduletest
推荐阅读
- android - 将应用程序从后台带到前台,Xamarin Android
- reactjs - 即使我的状态(用户)的 console.log 显示 3 条记录,react 语句中的react map 函数也只显示最后一条记录。有任何想法吗?
- c# - Aruba PEC 和附件的 Smtp 客户端 C# 问题
- python-3.x - 如何将 python/djanjo shell 作为脚本运行
- filter - 如何在 Google Analytics 中过滤掉我自己的点击?
- c++ - 获取 MPI_ERR_TRUNCATE:针对 MPI_Gatherv 截断消息
- r - 如何在 R 中创建一个函数来计算纵向研究参与者的每周调查?
- ethereum - 错误:返回错误:处理事务时出现 VM 异常:使用 New 创建时恢复
- python - 将数据框列添加到特定位置
- git - 提交在许多子模块中引入了重大更改。我可以使用什么可视化工具来比较所有子模块中的先前提交?