在这里用到的是xlwt
import xlwt
如果还未安装此模块,可以执行下面的命令安装:
pip install xlwt
接下来就是将数据列表存储到excel当中:
def save_to_excel():
try:
workbook = xlwt.Workbook(encoding='utf-8')
sheet = workbook.add_sheet('taobao_food')
head = ['标题', '图片地址', '价格', '交易数', '商店', '地址'] #表头
for h in range(len(head)):
sheet.write(0, h, head[h])
i = 1
for product in all_products:
sheet.write(i, 0, product['title'])
sheet.write(i, 1, product['image'])
sheet.write(i, 2, product['price'])
sheet.write(i, 3, product['deal'])
sheet.write(i, 4, product['shop'])
sheet.write(i, 5, product['location'])
i+=1
workbook.save('/Users/xxx/Desktop/tb_pt.xls')
print('写入excel成功')
except Exception:
print('写入excel失败')
其中 all_products 是包含上述6个字段的产品列表
xlwt用于写excel, xlrd用于读取excel中的数据,这里没有用到xlrd, 后续用到的时候继续更新
网友评论