Scrapy爬虫集成MongoDB存储

1:在settings.py文件中添加MongoDB相关配置:

python 复制代码
# settings.py

# MongoDB配置
MONGO_URI = 'mongodb://localhost:27017'  # MongoDB连接字符串
MONGO_DATABASE = 'yiche_cars'  # 数据库名称
MONGO_COLLECTION = 'car_info'  # 集合名称

2:创建MongoDB管道:

python 复制代码
# pipelines.py

import pymongo
from itemadapter import ItemAdapter
from scrapy.exceptions import DropItem

class MongoDBPipeline:
    def __init__(self, mongo_uri, mongo_db, collection_name=None):
        self.mongo_uri = mongo_uri
        self.mongo_db = mongo_db
        self.collection_name = collection_name  # 可选:自定义集合名
        self.client = None
        self.db = None

    @classmethod
    def from_crawler(cls, crawler):
        return cls(
            mongo_uri=crawler.settings.get('MONGO_URI'),
            mongo_db=crawler.settings.get('MONGO_DATABASE', 'scrapy_db'),
            collection_name=crawler.settings.get('MONGO_COLLECTION')  # 可选
        )

    def open_spider(self, spider):
        try:
            self.client = pymongo.MongoClient(self.mongo_uri, serverSelectionTimeoutMS=5000)
            self.db = self.client[self.mongo_db]
            # 测试连接
            self.client.server_info()
            spider.logger.info("成功连接MongoDB!")
        except pymongo.errors.ServerSelectionTimeoutError as err:
            spider.logger.error('MongoDB连接失败: %s', err)
            raise DropItem("无法连接MongoDB")

    def close_spider(self, spider):
        if self.client:
            self.client.close()

    def process_item(self, item, spider):
        # 如果设置了 collection_name,优先使用它,否则使用 spider.name
        collection_name = self.collection_name if self.collection_name else spider.name
        
        try:
            self.db[collection_name].insert_one(ItemAdapter(item).asdict())
            spider.logger.debug(f"Item 写入 MongoDB: {self.mongo_db}/{collection_name}")
        except pymongo.errors.PyMongoError as e:
            spider.logger.error("写入MongoDB错误: %s", e)
            raise DropItem("写入数据库失败")
        
        return item  # 必须返回 item,否则后续 pipeline 无法处理

3:在settings.py中启用MongoDB管道:

python 复制代码
# settings.py

ITEM_PIPELINES = {
    'spt_spider.pipelines.MongoPipeline': 300,
    # 其他管道...
}

运行爬虫:

scrapy crawl yiche

相关推荐
AI全栈实验室4 天前
MongoDB迁移金仓踩了5个坑,最后一个差点回滚
mongodb
cipher5 天前
crawl4ai:AI时代的数据采集利器——从入门到实战
后端·爬虫·python
数据知道5 天前
MongoDB 元素查询运算符:使用 `$exists` 检查字段是否存在及处理缺失字段
数据库·mongodb
数据知道5 天前
MongoDB 批量写操作:`bulkWrite()` 在数据迁移与清洗中的高性能应用
数据库·mongodb
数据知道5 天前
MongoDB 数组更新操作符:`$push`、`$pull`、`$addToSet` 管理列表数据
数据库·mongodb
数据知道5 天前
MongoDB 更新操作符 `$set` 与 `$unset`:精准修改字段与删除字段
数据库·mongodb
数据知道5 天前
MongoDB 数值更新原子操作:`$inc` 实现点赞、计数器等高并发原子操作
数据库·算法·mongodb
深蓝电商API5 天前
结构化数据提取:XPath vs CSS 选择器对比
爬虫·python
易辰君6 天前
【Python爬虫实战】正则:中文匹配与贪婪非贪婪模式详解
开发语言·爬虫·python
深蓝电商API6 天前
爬虫增量更新:基于时间戳与哈希去重
爬虫·python