from openpyxl import Workbook wk = Workbook() #class实例化 ws = wk.active #激活工作表 ws["A1"] = 999999 #A1表格输入数据 ws.append(['2018-1-1','学习','Python','人生苦短,我用Python']) #加入一行数据 wb.save('/me/con.xlsx') #保存文件
from openpyxl import Workbook class TuniuPipeline(object): # 设置工序一 self.wb = Workbook() self.ws = self.wb.active self.ws.append(['新闻标题', '新闻链接', '来源网站', '发布时间', '相似新闻', '是否含有网站名']) # 设置表头 def process_item(self, item, spider): # 工序具体内容 line = [item['title'], item['link'], item['source'], item['pub_date'], item['similar'], item['in_title']] # 把数据中每一项整理出来 self.ws.append(line) # 将数据以行的形式添加到xlsx中 self.wb.save('/home/alexkh/tuniu.xlsx') # 保存xlsx文件 return item
为了让pipeline.py
生效,还需要在settings.py
文件中增加设置,内容如下:
ITEM_PIPELINES = { 'tuniunews.pipelines.TuniuPipeline': 200, # 200是为了设置工序顺序 }