为什么Python报错"ImportError: No module named linkextractor",即使已安装Scrapy模块?

3 投票
3 回答
3237 浏览
提问于 2025-04-18 01:22

我想安装portia。这个东西需要用到scrapyslybot。我还按照推荐安装了virtualenv,并执行了virtualenv Portia来为这个项目创建一个专用的Python虚拟环境。现在我需要用twistd -n slyd来启动twisted服务器,但它却报了个错:

  File "/home/john/Downloads/portia-master/slybot/slybot/linkextractor/__init__.py", line 6, in <module>
    from .base import BaseLinkExtractor, ALLOWED_SCHEMES   File "/home/john/Downloads/portia-master/slybot/slybot/linkextractor/base.py", line 6, in <module>
    from scrapy.linkextractor import IGNORED_EXTENSIONS ImportError: No module named linkextractor

我用的是Ubuntu 12.04,安装了:python 2.7, pip, python-scrapy, python-twisted

这里可能出什么问题呢?我之前没有用过pipvirtualenv,感觉在我上面描述的部署流程中可能有什么不对的地方(比如环境变量,其他virtualenv/pip的配置)?

3 个回答

-2

嗯,我觉得你应该看看这个问题(https://github.com/scrapinghub/portia/issues/13)在GitHub的scrapinghub仓库里。

我解决了一些导入错误,方法是在我的系统上安装库,而不是在虚拟环境中安装。

在我的情况下,涉及的库是jsonschema(pip install jsonschema)和slybot(pip install -e slybot),然后我运行了requirements.txt。

祝好!

0

它缺少了中间的部分;contrib,而linkextractor是它的一个子模块。

应该是这样的:

scrapy.contrib.linkextractor
       ^^^^^^^
3

在Python3中,只需要在的后面加上一个

把:

from scrapy.linkextractor import LinkExtractor

换成:

from scrapy.linkextractors import LinkExtractor
                         ^

然后就可以享受这个功能了。

想了解更多信息,可以查看Scrapy的文档,点击这里

撰写回答