# 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


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import openpyxl

class HomeworkPipeline:

    # 初始化方法
    def __init__(self):
        # 工作簿对象
        self.wb = openpyxl.Workbook()
        # 创建新的工作表
        # wb.create_sheet()
        # 获取默认激活的工作表
        self.ws = self.wb.active
        # 修改表名
        self.ws.title = '采集文章数据'
        # self.ws.row_dimensions[1].height = 100
        self.ws.column_dimensions['A'].width = 80
        self.ws.append(('标题', '作者', '来源', '关键字', '时间', '正文'))

    def close_spider(self, spider):
        self.wb.save('采集结果.xlsx')

    # 专门用来处理item
    # 每接收到一个item就会被调用一次
    def process_item(self, item, spider):
        title = item.get('title', '')
        author = item.get('author', '')
        source = item.get('source', '')
        keyword = item.get('keyword', '')
        time = item.get('time', '')
        content = item.get('content', '')

        self.ws.append((title, author, source, keyword, time, content))
        return item
