想要批量发送Get请求获取api返回的数据,并且将数据导出为excel表格
headers = {
"Authorization": "Bearer " + token
}
with open("url.txt", "r", encoding="utf-8") as read_url:
url_get = read_url.readlines()
for li in url_get:
response = requests.get(url=li, headers=headers)
content_data_total = json.loads(data_num_response.text)["data"]["total"]
result = {'taskId':li.replace("*****", "").replace("&size=1", "").replace("\n",""),
'data_amount':content_data_total}
print(result)
打印的数据是这样的
尝试过的方法
def data_excel():
data_f = pd.DataFrame(list(result_get_notexported_data_num))
# 指定字段顺序
data_order = ["taskId", "data_amount"]
data_f = data_f[data_order]
# 将列名转换为中文
data_columns_map = {
"taskId": "任务Id",
"data_amount": "总量",
}
data_f.rename(columns=data_columns_map, inplace=True)
# 指定生成的表格名称
data_filepath = pd.ExcelWriter(current_date + " 数据.xlsx")
# 替换空单元格
data_f.fillna("", inplace=True)
# 输出
data_f.to_excel(data_filepath, encoding="utf-8", index=False)
# 保存表格
data_filepath.save()
# 转写为html
data_html_f = pd.read_excel(current_date + " 数据.xlsx")
data_html_f2 = status_html_f.drop(labels="任务Id", axis=1) # 删除taskId列
data_html_f2.to_html(current_date + " 数据.html", encoding="utf_8_sig", header=True,
index=False)
if __name__ == '__main__':
data_excel()
报错:
**raise KeyError(f"None of [{key}] are in the [{axis_name}]")
KeyError: "None of [Index(['taskId', 'data_amount'], dtype='object')] are in the [columns]"**
我想要把这些数据以taskId和data amount为表头,写入excel表格中,并将excel表格转换为html格式,请教该如何实现!谢谢!