微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

scrapy数据保存为excel

一、概述

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

原文地址:https://cloud.tencent.com/developer/article/1817509

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐