Scrapy中报错"No module named items"

现象

在pieline.py文件中引入同级目录的items模块中的类, 运行时会报错"No module named items"
如下图注释行所示.


示例截图.png

代码如下:

# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymongo
# from items import PositionItem, QqhrItem
from QQHR.items import PositionItem, QqhrItem


class TencentListPipeline(object):
    def open_spider(self, spider):
        self.conn = pymongo.MongoClient().tencent.list

    def process_item(self, item, spider):
        if isinstance(item, QqhrItem):
            self.conn.insert(dict(item))

        return item


class TencentDetailPipeline(object):
    def open_spider(self, spider):
        self.conn = pymongo.MongoClient().tencent.detail

    def process_item(self, item, spider):
        if isinstance(item, PositionItem):
            self.conn.insert(dict(item))

        return item

解决方法

用上级文件夹的名字引入, 如上图所示.
本文参考了这篇文章

你可能感兴趣的:(Scrapy中报错"No module named items")