[爬虫]两种方法openpyxl和xlwt

第一种方法:附上openpyxl官方使用文档  我比较喜欢这种方式,简单、效率高

from openpyxl import Workbook
wb = Workbook()

# grab the active worksheet
ws = wb.active

# Data can be assigned directly to cells
ws['A1'] = 33

# Rows can also be appended
ws.append([1, 2, 3])

# Python types will automatically be converted
import datetime
ws['A2'] = datetime.datetime.now()

# Save the file
wb.save("1应用openpyxl将爬取数据存入excel.xlsx")

 

第二种方法:xlwt官方使用文档

import xlwt #只能打开.xls文件
from datetime import datetime

style0 = xlwt.easyxf('font: name Times New Roman, color-index red, bold on',
    num_format_str='#,##0.00')
style1 = xlwt.easyxf(num_format_str='D-MMM-YY')

wb = xlwt.Workbook()
ws = wb.add_sheet('A Test Sheet')

#给单元格赋值,把单元格当成坐标,第一个单元格坐为(0,0)
ws.write(0, 0, 1234.56, style0)#
ws.write(1, 0, datetime.now(), style1)
ws.write(2, 0, 1)
ws.write(2, 1, 1)
ws.write(2, 2, xlwt.Formula("A3+B3"))

wb.save('2应用xlwt将爬取数据存入excel.xls')

 

你可能感兴趣的:(爬虫)