我正在使用 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 上。
我的项目结构是:
最佳答案
你可以做几件事:
第一
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/