我直接运行单文件,并没有导入错误,此外,我单独在py文件中使用mongodb也正常,但是在scrapy项目中运行,会说导入失败,这是为什么?
import json
import pymongo
from scrapy.utils.project import get_project_settings
class MyscrapyPipeline(object):
def __init__(self):
settings = get_project_settings()
client = pymongo.MongoClient(host=settings['MONGODB_HOST'], port=settings['MONGODB_PORT'])
db = client[settings['MONGODB_DBNAME']]
self.mongo = db[settings['MONGODB_COLNAME']['taptap']]
def process_item(self, item, spider):
""" 处理数据 """
for k, v in item.items():
item[k] = ''.join(v.split()) if bool(type(v) is not list) else v
self.mongo.insert_one(json.dumps(dict(item), ensure_ascii=False))
return item
相关分类