Scrapy与MongoDB的异步数据存储

在数据采集过程中,处理大量的数据请求和存储任务是常见的需求。使用Scrapy来爬取数据并将其存储到MongoDB中是一个高效的解决方案。本文将介绍如何实现一个异步插入MongoDB的Scrapy管道。

项目背景

在本项目中,我们需要从某些公开网站上爬取数据,并将这些信息异步存储到MongoDB数据库中。为了提高性能,我们可以采用异步操作。这不仅能够提升处理速度,还能更好地利用系统资源。

Scrapy与异步MongoDB客户端

我们将使用motor库,它是一个异步MongoDB驱动,能够与asyncio很好地结合,实现异步的MongoDB操作。通过Scrapy的管道,我们可以在处理爬取到的数据时,直接将其存储到MongoDB中。

实现步骤

1. 安装依赖

首先,我们需要安装motor库:

python 复制代码
pip install motor

2. Scrapy管道实现

以下是我们的ScrapyPipeline类的实现,它实现了从Scrapy爬虫到MongoDB的异步数据插入。

python 复制代码
import motor.motor_asyncio
from scrapy.utils.project import get_project_settings

class ScrapyPipeline:
    def __init__(self, host, port, db_name, collection_name):
        self.host = host
        self.port = port
        self.db_name = db_name
        self.collection_name = collection_name
        self.client = None

    @classmethod
    def from_crawler(cls, crawler):
        settings = crawler.settings
        return cls(
            host=settings.get("MONGODB_HOST"),
            port=settings.getint("MONGODB_PORT"),
            db_name=settings.get("MONGODB_DB"),
            collection_name=settings.get("MONGODB_LIST_PRODUCT_COL")
        )

    def open_spider(self, spider):
        print('爬虫开始')
        self.client = motor.motor_asyncio.AsyncIOMotorClient(host=self.host, port=self.port)

    async def process_item(self, item, spider):
        item = dict(item)
        await self.client[self.db_name][self.collection_name].insert_one(item)
        return item

    def close_spider(self, spider):
        print('爬虫结束')
        self.client.close()

3. 配置Scrapy项目

在Scrapy项目的settings.py文件中,添加MongoDB的配置信息:

python 复制代码
MONGODB_HOST = 'localhost'
MONGODB_PORT = 27017
MONGODB_DB = 'SpiderProject'
MONGODB_LIST_PRODUCT_COL = 'test_data'

同时,启用我们自定义的管道:

python 复制代码
ITEM_PIPELINES = {
    'myproject.pipelines.ScrapyPipeline': 300,
}

4. 解释关键部分

@classmethod from_crawler(cls, crawler)

这个方法是Scrapy的约定方法,用于从Scrapy的设置中创建管道实例。通过这个方法,我们可以将Scrapy的设置传递给管道类。

python 复制代码
@classmethod
def from_crawler(cls, crawler):
    settings = crawler.settings
    return cls(
        host=settings.get("MONGODB_HOST"),
        port=settings.getint("MONGODB_PORT"),
        db_name=settings.get("MONGODB_DB"),
        collection_name=settings.get("MONGODB_LIST_PRODUCT_COL")
    )
open_spider(self, spider)

在爬虫开始时,连接到MongoDB:

python 复制代码
def open_spider(self, spider):
    print('爬虫开始')
    self.client = motor.motor_asyncio.AsyncIOMotorClient(host=self.host, port=self.port)
    self.db = self.client[self.db_name]
process_item(self, item, spider)

这是异步处理每个item的方法,将item插入到MongoDB中:

python 复制代码
async def process_item(self, item, spider):
    item = dict(item)
    await self.db[self.collection_name].insert_one(item)
    return item
close_spider(self, spider)

在爬虫结束时,关闭MongoDB连接:

python 复制代码
def close_spider(self, spider):
    print('爬虫结束')
    self.client.close()

总结

通过以上步骤,我们实现了一个异步的Scrapy管道,用于将爬取的数据存储到MongoDB中。这种方式不仅提高了数据处理的效率,还能充分利用系统资源。希望这篇文章能帮助你更好地理解和实现Scrapy与MongoDB的异步数据存储。

作者:pycode

链接:https://juejin.cn/post/7379884568579457051

相关推荐
这里有鱼汤7 分钟前
95%开发者未充分利用的Python特性:解包操作性能实测与最佳实践
python
这里有鱼汤8 分钟前
🎨 Matplotlib十大高级绘图技巧,学会了升职加薪(不骗你)
后端·python
令狐少侠201111 分钟前
python后端程序部署到服务器 Ubuntu并配合 Vue 前端页面运行
服务器·前端·python
Anarkh_Lee30 分钟前
Python 项目环境配置与 Vanna 安装避坑指南 (PyCharm + venv)
人工智能·python·pycharm
付出不多43 分钟前
python函数与模块
开发语言·python
樱花穿过千岛湖1 小时前
第一章:Model Context Protocol (MCP)
网络·人工智能·python·网络协议·学习·tcp/ip
wolf犭良1 小时前
37、aiomysql实操习题
开发语言·python·算法
正经码农3 小时前
字典与集合——测试界的黑话宝典与BUG追捕术
python
nuclear20117 小时前
Python 从PPT文档中提取图片和图片信息(坐标、宽度和高度等)
python·powerpoint·ppt图片提取·提取ppt背景图片·提取pp所有图片
樱花穿过千岛湖8 小时前
第六章:Multi-Backend Configuration
人工智能·python·gpt·学习·ai