无法在scrapy crawler中导入我自己的模块 [英] Cannot import my own module in scrapy crawler

查看:63
本文介绍了无法在scrapy crawler中导入我自己的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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 行,在导入模块测试

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

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中导入我自己的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆