随笔 - 31  文章 - 0  评论 - 0  阅读 - 2444

<4> pipeline

复制代码
"""scrapy 保存管道数据"""
from scrapy.exporters import CsvItemExporter


class CsvPipeline:

    def __init__(self):
        # 文件存储初始化操作
        self.file = open('filename.csv', 'wb')
        self.exporter = CsvItemExporter(file=self.file, include_headers_line=True)
        self.exporter.start_exporting()

    def process_item(self, item, spider):
        self.exporter.export_item(item)
        return item

    def close_spider(self, spider):
        self.exporter.finish_exporting()
        self.file.close()


"""scrapy 管道去重保存""" import json import hashlib from scrapy.exceptions import DropItem from scrapy.exporters import CsvItemExporter class DupefiltePipeline: def __init__(self): # 去重集合 self.fingerprints = set() # 文件存储初始化操作 self.file = open('filename.csv', 'wb') self.exporter = CsvItemExporter(file=self.file, include_headers_line=True) self.exporter.start_exporting() def process_item(self, item, spider): fp = self.item_fingerprint(item) if fp in self.fingerprints: raise DropItem('duperfilters') self.fingerprints.add(fp) self.exporter.export_item(item) return item def item_fingerprint(self, item): dump_item = json.dumps(item) return hashlib.md5(bytes(dump_item, encoding='utf-8')).digest().hex() def close_spider(self, spider): self.exporter.finish_exporting() self.file.close()
"""scrapy 管道保存到MongoDB""" class BookPipeline: # 开始爬虫 def open_spider( self, spider ): print( " open. . . ") # 定义一个变盘,记录爬取的数量 self.count = 0 try: # 1. 获取MongoD8客户端 self.client - Mongoclient( " 1ocalhost" , 27817) # 2. 获取数据库 self.db = self.client.dangdang # 3.获取数据集合 self.collection = self.db.collection_dangdang # 标识 self.opend = True except BaseException as e : print(e) self.opend = Flase # 执行数据棱型的处理 def process_item( se1f,item,spider ): if self.opened: self.collection.insert_one(dict(item)) self.count += 1 return item # 结束爬虫 def close_spider( self, spider) : print(f"爬取了{self.count}条数据") print('closed...') if self.opend: # 说明: 如果数据库打开成立了,爬虫结束后,就掠数据库连接关闭棹 self.client.close()
复制代码
posted on   不是霉蛋  阅读(37)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示