Eamonn

人生苦短,我用Python

· 默认分类 · · 126次浏览

Scrapy多个spiders同时写入表格pipline

from openpyxl import Workbook

class ExcelPipeline(object):
    def __init__(self):
        self.wb = Workbook()
        self.ws =self.wb.active
        self.check=False

    def process_item(self,item, spider):
        if item:
            if not self.check:
                self.ws.append(list(item.keys()))
                self.check=True

            line = list(item.values())

            os.chdir(r'path')

            self.ws.append(line)
            self.wb.save(f"{'文件名'}.xlsx")
            return item
评论 (0条)