问题描述
我正在使用 Scrapy 编写爬虫程序.我已经构建了一个爬虫,它运行良好.
I'm writing a crawler using Scrapy. I've built a crawler and it works very well.
现在我想创建自己的模块,但总是收到此错误:
Now I want to create my own modules, but I always receive this error:
文件D:\Projects\bitbucket\terranoha\crawl1\crawl1\spiders\samplecrawler.py",第 4 行,在导入模块测试
ModuleNotFoundError: 没有名为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.
I am running: scrapy crawl --nolog samplecrawler
. I'm on Windows 10.
我的项目结构是:
推荐答案
你可以做几件事:
第一
from crawl1.spiders.moduletest import mythings
正如@elRuLL 所建议的
As suggested by @elRuLL
第二
from .moduletest import mythings
这通常是一个糟糕而脆弱的解决方案,但有可能.
This generally a bad and brittle solution but possible.
第三
你可以把它打包成包然后做.
You can package it as package and do.
init.py:
from spiders.moduletest import *
__all__ = [<Put your classes, methods, etc here>]
samplecrawler.py
import moduletest
这篇关于无法在scrapy crawler中导入我自己的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!