1. 程式人生 > >scrapy 爬取的資料儲存到exce表格中

scrapy 爬取的資料儲存到exce表格中

只需把爬取過來的資料yield出來,並在pipelines中定義表格即可。

from openpyxl import Workbook
from Job import settings

class JobPipeline(object):   # 設定工序一
    wb = Workbook()
    ws = wb.active
    ws.append(['title','addr','silary','needs','company','info','gm']) 

    def process_item(self, item, spider):  # 工序具體內容
        line = [item['title'][0],item['addr'][0],item['silary'][0],item['needs'][0],item['company'][0],item['info'][0],item['gm'][0]]  #把資料每一行整理出來
        self.ws.append(line)  # 將資料一行的形式新增到xlsx中
        self.wb.save('job.xlsx')  # 儲存xlsx檔案
        return item