当前位置:Gxlcms > 数据库问题 > MongoDB的连接

MongoDB的连接

时间:2021-07-01 10:21:17 帮助过:23人阅读

import pymongo

class MongoPipeline(object):

  collection_name = ‘表名‘

  def __init__(self, mongo_uri, mongo_db):

    self.mongo_uri = mongo_uri

    self.mongo_db = mongo_db

  @classmethod

  def from_crawler(cls, crawler):

    return cls(

      mongo_uri=crawler.settings.get(‘MONGO_URI‘),

      mongo_db=crawler.settings.get(‘MONGO_DATABASE‘, ‘items‘)

    )

  def open_spider(self, spider):

    self.client = pymongo.MongoClient(self.mongo_uri)

    self.db = self.client[self.mongo_db]

  def close_spider(self, spider):

    self.client.close()

  def process_item(self, item, spider):

    self.db[self.collection_name].insert_one(dict(item))

    return item

MongoDB的连接

标签:pipe   mongodb   uri   cli   col   client   method   tin   get   

人气教程排行