#获取课程成绩页面 defgetHtmlText(SemesterName,payload): respose = session.post(login_url,headers=headers,data=payload) print('服务器端返回码:',respose.status_code) if SemesterName is'1': SemesterName = g_20161 if SemesterName is'2': SemesterName = g_20162 if SemesterName is'3': SemesterName = g_20171 grade_url = StuGrade+SemesterName stugrade = session.get(grade_url,headers=headers) return stugrade.text #提取各科成绩 defGetFromText(txt): form = [] soup = BeautifulSoup(txt,'html.parser') for tr in soup.find('table').children: try: if isinstance(tr,element.Tag): tds = tr('td') Class = tds[1].string #课程 Credit = tds[4].string #学分 Grade = tds[5].string #成绩 form.append([''.join(Class.split()),''.join(Credit.split()),''.join(Grade.split())]) except: continue return form
#格式输出各科成绩 defprintgrade(ulist,num): tplt = "{0:{3}^25}\t{1:^10}\t{2:^10}\n" print(tplt.format("课程","学分","成绩",chr(12288))) file.write(tplt.format("课程","学分","成绩",chr(12288))) for i in range(num): u = ulist[i] print(tplt.format(u[0],u[1],u[2],chr(12288))) file.write(tplt.format(u[0],u[1],u[2],chr(12288)))
#提取网页中的课程表 defgetFormText(string): soup = BeautifulSoup(string,'html.parser') list = [] for tr in soup.find('table').children: try: time = tr('th')[0].string if time==None: continue day1 = tr('td')[0].string day2 = tr('td')[1].string day3 = tr('td')[2].string day4 = tr('td')[3].string day5 = tr('td')[4].string day6 = tr('td')[5].string day7 = tr('td')[6].string list.append([time,day1,day2,day3,day4,day5,day6,day7]) except: continue return list
#将提取到的课程表写入excel表格中 defWirteXls(list): worksheet.write(0,1,"星期一") worksheet.write(0,2,"星期二") worksheet.write(0,3,"星期三") worksheet.write(0,4,"星期四") worksheet.write(0,5,"星期五") worksheet.write(0,6,"星期六") worksheet.write(0,7,"星期日") for i in range(len(list)): u = list[i] for k in range(8): worksheet.write(i+1,k,u[k])