Pipeline.py

 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

 

posted @ 2019-03-04 14:17  瑞溪  阅读(121)  评论(0)    收藏  举报