本帖最后由 magicianly 于 2021-5-12 15:28 编辑
不知道为啥,感觉你们写的好复杂啊,我也来写一个吧,你们处理JSON数据的方式有点单一哦
import demjson
import requests
import xlwt
def write_excel_xls(path, sheet_name, value, title=None, isno=None):
"""
写Excel文件
:param path: 文件存放地址
:param sheet_name: 文档名称
:param title: 文档头
:param value: 一行的数据列表保存
:param isno: 是否序号
"""
count = 1
index = len(value) # 获取需要写入数据的行数
workbook = xlwt.Workbook() # 新建一个工作簿
sheet = workbook.add_sheet(sheet_name) # 在工作簿中新建一个表格
if title:
for t in range(len(title)):
sheet.write(0, t, title[t])
if isno:
for i in range(0, index):
sheet.write(i + 1, 0, count)
for j in range(0, len(value[i])):
sheet.write(i + 1, j + 1, value[i][j]) # 像表格中写入数据(对应的行和列)
count += 1
else:
for i in range(0, index):
for j in range(0, len(value[i])):
sheet.write(i + 1, j, value[i][j]) # 像表格中写入数据(对应的行和列)
workbook.save(path) # 保存工作簿
print("xls格式表格写入数据成功!")
url = 'https://sf-item.taobao.com/json/get_bid_records.htm?currentPage=1&id=641634330827&records_type=pageRecords'
r = requests.get(url)
datas = demjson.decode(r.text)
data_lst = list()
for data in datas.get('records'):
status_lst = data.get('status') # 状态
buyer_lst = data.get('alias') # 竞买人
price_lst = data.get('price') # 价格
time_lst = data.get('date') # 竞买时间
data_lst.append([status_lst, buyer_lst, price_lst, time_lst])
title = ['状态', '竞买人', '价格', '竞买时间']
write_excel_xls('./1.xls', 'test', data_lst, isno=True, title=title)
|