前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >scrapy数据保存为excel

scrapy数据保存为excel

作者头像
py3study
发布2021-04-25 10:54:32
1.2K0
发布2021-04-25 10:54:32
举报
文章被收录于专栏:python3python3

一、概述

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

二、实现方法

安装模块

代码语言:javascript
复制
pip3 install openpyxl

修改pipelines.py

代码语言:javascript
复制
# -*- 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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-04-23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、概述
  • 二、实现方法
    • 安装模块
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档