• scrapy连接MongoDB


    Scrapy中连接MongoDB所需要做的工作如下:

    1.settings中需要设置的部分:

    # 启动管道组件
    ITEM_PIPELINES = {
        'QianChengWuYu.mongoDBPiplelines.MongoDBPipline': 300,
    }
    # MonDB的连接参数
    MONGODB_URI = 'mongodb://localhost:27017'
    MONGODB_DB_NAME = 'scrapy_db'

         (1)其中ITEM_PIPELINES中设置的是QianChengWuyu项目中的管道文件mongoDBPiplelines中的MongoDBPipline类,优先级是300

         (2)其中MONGODB_URI为指定登录的IP 这里是localhost,端口为27017,MONGODB_DB_NAME为MongoDB中数据库名称

    2.scrapy中piplines文件如下:

    """
    该类用于mongdb数据库的数据插入
    """
    
    from pymongo import MongoClient
    from scrapy import Item
    
    class MongoDBPipline(object):
        def open_spider(self, spider):
            """
            该方法用于连接数据库
            """
            db_uri = spider.settings.get('MONGODB_URI', 'mongodb://localhost:27017')
            db_name = spider.settings.get('MONGODB_DB_NAME', 'scrapy_default')
    
            self.db_client = MongoClient('mongodb://localhost:27017')
            self.db = self.db_client[db_name]
    
        def close_spider(self, spider):
            """
            该方法用于关闭数据库
            """
            self.db_client.close()
    
        def process_item(self, item, spider):
            """
            该方法用于插入数据
            """
            self.insert_db(item)
    
            return item
    
        def insert_db(self, item):
            if isinstance(item, Item):
                item = dict(item)     # 将一项数据转化为字典格式
    
            self.db.books.insert_one(item)  # 向集合books中插入数据
  • 相关阅读:
    Neko's loop HDU-6444(网络赛1007)
    Parameters
    SETLOCAL
    RD / RMDIR Command
    devenv 命令用法
    Cannot determine the location of the VS Common Tools folder.
    'DEVENV' is not recognized as an internal or external command,
    How to change Visual Studio default environment setting
    error signing assembly unknown error
    What is the Xcopy Command?:
  • 原文地址:https://www.cnblogs.com/llssx/p/9082408.html
Copyright © 2020-2023  润新知