1 import pymongo 2 3 class MongoPipeline(object): 4 5 collection_name = ‘表名‘ 6 7 def __init__(self, mongo_uri, mongo_db): 8 9 self.mongo_uri = mongo_uri 10 self.mongo_db = mongo_db 11 12 @classmethod 13 14 def from_crawler(cls, crawler): 15 16 return cls( 17 18 mongo_uri=crawler.settings.get(‘MONGO_URI‘), 19 mongo_db=crawler.settings.get(‘MONGO_DATABASE‘, ‘items‘) 20 21 ) 22 23 def open_spider(self, spider): 24 self.client = pymongo.MongoClient(self.mongo_uri) 25 self.db = self.client[self.mongo_db] 26 27 def close_spider(self, spider): 28 self.client.close() 29 30 def process_item(self, item, spider): 31 self.db[self.collection_name].insert_one(dict(item)) 32 return item
原文:https://www.cnblogs.com/jswrui/p/10470410.html