I run the single file directly without import errors. In addition, it is normal for me to use mongodb in the py file alone, but when I run it in the scrapy project, I will say that the import failed. Why?
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