scrapy数据保存为excel

发布时间:2021-04-23 17:14:35编辑:admin阅读(10946)

    一、概述

    scrapy爬取的数据,需要保存到excel中,根据中文标题,将对应的数据写入。

     

    二、实现方法

    安装模块

    pip3 install openpyxl

     

    修改pipelines.py

    # -*- coding: utf-8 -*-
    
    # Define your item pipelines here
    #
    # Don't forget to add your pipeline to the ITEM_PIPELINES setting
    # See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
    from openpyxl import Workbook
    
    
    class ExcelPipeline(object):
        def __init__(self):
            self.wb = Workbook()
            self.ws = self.wb.active
            self.ws.append(['姓名', '年龄', '地址')
            self.file_name = "test.xlsx"
    
        def process_item(self, item, spider):
            line = [item['name'], item['age'], item['address']]
            self.ws.append(line)
            self.wb.save(self.file_name)
            return item
    
        def close_spider(self, spider):
            # 关闭
            self.wb.close()

     

    本文参考链接:

    https://blog.csdn.net/qq_42336560/article/details/80951401


关键字

上一篇: Fiddler的安装与使用

下一篇: 没有了