在這里用到的是xlwt
import xlwt
如果還未安裝此模塊乞巧,可以執(zhí)行下面的命令安裝:
pip install xlwt
接下來就是將數(shù)據(jù)列表存儲(chǔ)到excel當(dāng)中:
def save_to_excel():
try:
workbook = xlwt.Workbook(encoding='utf-8')
sheet = workbook.add_sheet('taobao_food')
head = ['標(biāo)題', '圖片地址', '價(jià)格', '交易數(shù)', '商店', '地址'] #表頭
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個(gè)字段的產(chǎn)品列表
xlwt用于寫excel没卸, xlrd用于讀取excel中的數(shù)據(jù),這里沒有用到xlrd谤专, 后續(xù)用到的時(shí)候繼續(xù)更新