首页 > 其他 > 详细

Pipeline.py

时间:2019-03-04 14:35:29      阅读:159      评论:0      收藏:0      [点我收藏+]
 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

 

Pipeline.py

原文:https://www.cnblogs.com/jswrui/p/10470410.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!