# 2、http: // doc.nnzhp.cn / index.php?s = / 6 & page_id = 14
# 调用获取学生信息的接口,保存到excel里面
import requests,xlwt
def stu():
url='http://api.nnzhp.cn/api/user/all_stu'
header={'Referer':'http://api.nnzhp.cn/'}
req=requests.get(url,headers=header)
return(req.json())
book=xlwt.Workbook()
sheet=book.add_sheet('sheet1')
lst=['id','name','sex','age','addr','grade','phone','gold']
for col,field in enumerate(lst):
sheet.write(0,col,field)
for num,line in enumerate(stu()['stu_info']):
for k,v in line.items():
sheet.write(num+1,lst.index(k),v)
#在序列中检索参数并返回第一次出现的索引,没找到就会报错。(lst.index(k))
book.save('stu.xls')