|
| 1 | +#!/usr/bin/env python |
| 2 | +#-*- coding: utf-8 -*- |
| 3 | + |
| 4 | +__author__ = 'ZYSzys' |
| 5 | + |
| 6 | +import requests |
| 7 | +import re |
| 8 | +import os |
| 9 | +import sys |
| 10 | +import urllib |
| 11 | +import getpass |
| 12 | +from lxml import etree |
| 13 | +from PIL import Image |
| 14 | +from imp import reload |
| 15 | +from bs4 import BeautifulSoup |
| 16 | + |
| 17 | + |
| 18 | +class Who: |
| 19 | + def __init__(self, user, pswd): |
| 20 | + self.user = user |
| 21 | + self.pswd = pswd |
| 22 | + |
| 23 | + |
| 24 | +class Tool: |
| 25 | + rma = re.compile('<a href=.*?>|</a>') |
| 26 | + rmtb = re.compile('<br />|</br>|<br>') |
| 27 | + rmtr = re.compile('<td>|</td>|<tr>|</tr>|<tr class="alt">|<tr class="datelisthead">') |
| 28 | + rmtime1 = re.compile('<td align="Center" width="7%">.*?</td>') |
| 29 | + rmtime2 = re.compile('<td class="noprint" align="Center".*?>.*?</td>') |
| 30 | + |
| 31 | + def replace(self, x): |
| 32 | + x = re.sub(self.rma, ' ', x) |
| 33 | + x = re.sub(self.rmtb, '---', x) |
| 34 | + x = re.sub(self.rmtr, ' ', x) |
| 35 | + x = re.sub(self.rmtime1, '\n', x) |
| 36 | + x = re.sub(self.rmtime2, '', x) |
| 37 | + return x.strip() |
| 38 | + |
| 39 | + |
| 40 | +def Getgrade(response): |
| 41 | + html = response.content |
| 42 | + soup = BeautifulSoup(html, 'lxml') |
| 43 | + trs = soup.find(id="Datagrid1").findAll("tr") |
| 44 | + Grades = [] |
| 45 | + keys = [] |
| 46 | + tds = trs[0].findAll("td") |
| 47 | + tds = tds[:2] + tds[3:5] + tds[6:9] |
| 48 | + for td in tds: |
| 49 | + keys.append(td.string) |
| 50 | + for tr in trs[1:]: |
| 51 | + tds = tr.findAll("td") |
| 52 | + tds = tds[:2] + tds[3:5] + tds[6:9] |
| 53 | + values = [] |
| 54 | + for td in tds: |
| 55 | + values.append(td.string) |
| 56 | + one = dict((key, value) for key, value in zip(keys, values)) |
| 57 | + Grades.append(one) |
| 58 | + return Grades |
| 59 | + |
| 60 | + |
| 61 | +def Getgradetestresults(trs): |
| 62 | + results = [] |
| 63 | + k = [] |
| 64 | + for td in trs[0].xpath('.//td/text()'): |
| 65 | + k.append(td) |
| 66 | + trs = trs[1:] |
| 67 | + for tr in trs: |
| 68 | + tds = tr.xpath('.//td/text()') |
| 69 | + v = [] |
| 70 | + for td in tds: |
| 71 | + v.append(td) |
| 72 | + one = dict((i, j) for i, j in zip(k, v)) |
| 73 | + results.append(one) |
| 74 | + return results |
| 75 | + |
| 76 | + |
| 77 | +class University: |
| 78 | + def __init__(self, student, baseurl): |
| 79 | + reload(sys) |
| 80 | + self.student = student |
| 81 | + self.baseurl = baseurl |
| 82 | + self.session = requests.session() |
| 83 | + self.session.headers['User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36' |
| 84 | + |
| 85 | + def Login(self): |
| 86 | + url = self.baseurl+'/default2.aspx' |
| 87 | + res = self.session.get(url) |
| 88 | + cont = res.content |
| 89 | + selector = etree.HTML(cont) |
| 90 | + __VIEWSTATE = selector.xpath('//*[@id="form1"]/input/@value')[0] |
| 91 | + imgurl = self.baseurl + '/CheckCode.aspx' |
| 92 | + imgres = self.session.get(imgurl, stream=True) |
| 93 | + img = imgres.content |
| 94 | + with open('code.jpg', 'wb') as f: |
| 95 | + f.write(img) |
| 96 | + jpg = Image.open('{}/code.jpg'.format(os.getcwd())) |
| 97 | + jpg.show() |
| 98 | + jpg.close |
| 99 | + code = input('输入验证码:') |
| 100 | + RadioButtonList1 = u"学生" |
| 101 | + data = { |
| 102 | + "__VIEWSTATE": __VIEWSTATE, |
| 103 | + "txtUserName": self.student.user, |
| 104 | + "TextBox1": self.student.pswd, |
| 105 | + "TextBox2": self.student.pswd, |
| 106 | + "txtSecretCode": code, |
| 107 | + "RadioButtonList1": RadioButtonList1, |
| 108 | + "Button1": "", |
| 109 | + "lbLanguage": "" |
| 110 | + } |
| 111 | + loginres = self.session.post(url, data=data) |
| 112 | + logcont = loginres.text |
| 113 | + pattern = re.compile( |
| 114 | + '<form name="Form1".*?action=(.*?) id="Form1">', re.S) |
| 115 | + res = re.findall(pattern, logcont) |
| 116 | + try: |
| 117 | + if res[0][17:29] == self.student.user: |
| 118 | + print('Login succeed!') |
| 119 | + except: |
| 120 | + print('Login failed! Maybe Wrong password ! ! !') |
| 121 | + return |
| 122 | + pattern = re.compile('<span id="xhxm">(.*?)</span>') |
| 123 | + xhxm = re.findall(pattern, logcont) |
| 124 | + name = xhxm[0].replace('同学', '') |
| 125 | + self.student.urlname = urllib.parse.quote_plus(str(name)) |
| 126 | + return True |
| 127 | + |
| 128 | + def GetClass(self): |
| 129 | + self.session.headers['Referer'] = self.baseurl + \ |
| 130 | + '/xs_main.aspx?xh=' + self.student.user |
| 131 | + kburl = self.baseurl + '/xskbcx.aspx?xh='+self.student.user + \ |
| 132 | + '&xm='+self.student.urlname+'&gnmkdm=N121603' |
| 133 | + kbresponse = self.session.get(kburl) |
| 134 | + kbcont = kbresponse.text |
| 135 | + pattern = re.compile('<td.*?align="Center".*?>(.*?)</td>', re.S) |
| 136 | + contents = re.findall(pattern, kbcont) |
| 137 | + tool = Tool() |
| 138 | + f = open(os.getcwd()+'/zhengfang.txt', 'w') |
| 139 | + f.write(u'本学期课表:'+'\n') |
| 140 | + cnt = 1 |
| 141 | + l = [u'周一', u'周二', u'周三', u'周四', u'周五', u'周六', u'周日'] |
| 142 | + for day in l: |
| 143 | + for i in contents: |
| 144 | + if u'星期' in i: |
| 145 | + continue |
| 146 | + elif u'第' in i: |
| 147 | + if day in i: |
| 148 | + con = tool.replace(i) |
| 149 | + f.write(str(cnt)+':\t'+con+'\n') |
| 150 | + cnt += 1 |
| 151 | + else: |
| 152 | + continue |
| 153 | + f.write('\n') |
| 154 | + f.close() |
| 155 | + print('Download class succeed!') |
| 156 | + |
| 157 | + def GetGrade(self): |
| 158 | + self.session.headers['Referer'] = self.baseurl + \ |
| 159 | + '/xs_main.aspx?xh=' + self.student.user |
| 160 | + gradeurl = self.baseurl + '/xscjcx.aspx?xh='+self.student.user + \ |
| 161 | + '&xm='+self.student.urlname+'&gnmkdm=N121605' |
| 162 | + graderesponse = self.session.get(gradeurl) |
| 163 | + gradecont = graderesponse.content |
| 164 | + soup = BeautifulSoup(gradecont, 'lxml') |
| 165 | + __VIEWSTATE = soup.findAll(name="input")[2]["value"] |
| 166 | + self.session.headers['Referer'] = gradeurl |
| 167 | + data = { |
| 168 | + "__EVENTTARGET": "", |
| 169 | + "__EVENTARGUMENT": "", |
| 170 | + "__VIEWSTATE": __VIEWSTATE, |
| 171 | + "hidLanguage": "", |
| 172 | + "ddlXN": "", |
| 173 | + "ddlXQ": "", |
| 174 | + "ddl_kcxz": "", |
| 175 | + "btn_zcj": u'历年成绩' |
| 176 | + } |
| 177 | + grares = self.session.post(gradeurl, data=data) |
| 178 | + grades = Getgrade(grares) |
| 179 | + totup = 0 |
| 180 | + totdown = 0 |
| 181 | + f = open(os.getcwd()+'/zhengfang.txt', 'a+') |
| 182 | + f.write('\n\n\n'+u'历年成绩:'+'\n') |
| 183 | + for i in grades[0]: |
| 184 | + f.write('%-13s\t' % i) |
| 185 | + f.write('\n') |
| 186 | + for each in grades: |
| 187 | + for one in each: |
| 188 | + f.write('%-15s\t' % each[one]) |
| 189 | + f.write('\n') |
| 190 | + totup = totup + float(each[u'绩点']) * float(each[u'学分']) |
| 191 | + totdown = totdown + float(each[u'学分']) |
| 192 | + f.write('\n'+u'平均绩点: '+'%.2f\t\t\t' % (totup / totdown) + |
| 193 | + u'总学分绩点: '+'%.2f\t\t\t' % totup + u'总学分: '+'%.2f\n' % totdown) |
| 194 | + f.close() |
| 195 | + print('Download grade succeed!') |
| 196 | + |
| 197 | + def GradeTestResults(self): |
| 198 | + self.session.headers['Referer'] = self.baseurl + \ |
| 199 | + '/xs_main.aspx?xh=' + self.student.user |
| 200 | + gtrurl = self.baseurl + '/xsdjkscx.aspx?xh='+self.student.user + \ |
| 201 | + '&xm='+self.student.urlname+'&gnmkdm=N121606' |
| 202 | + gtrresponse = self.session.get(gtrurl) |
| 203 | + gtrcontent = gtrresponse.text |
| 204 | + gtrhtml = etree.HTML(gtrcontent) |
| 205 | + trs = gtrhtml.xpath('//table[@class="datelist"]/tr') |
| 206 | + f = open(os.getcwd()+'/zhengfang.txt', 'a+') |
| 207 | + f.write('\n\n\n'+u'等级考试成绩:'+'\n') |
| 208 | + results = Getgradetestresults(trs) |
| 209 | + for one in results[0]: |
| 210 | + f.write('%-10s\t' % one) |
| 211 | + f.write('\n') |
| 212 | + for each in results: |
| 213 | + for one in each: |
| 214 | + f.write('%-10s\t' % each[one]) |
| 215 | + f.write('\n') |
| 216 | + f.close() |
| 217 | + print('Download grade test results succeed!') |
| 218 | + |
| 219 | + |
| 220 | +if __name__ == "__main__": |
| 221 | + url = input("学校教务网站(如http://115.236.84.162):") |
| 222 | + user = input("学号:") |
| 223 | + pswd = getpass.getpass("密码:") |
| 224 | + who = Who(user, pswd) |
| 225 | + univ = University(who, url) |
| 226 | + if univ.Login(): |
| 227 | + univ.GetClass() |
| 228 | + univ.GetGrade() |
| 229 | + univ.GradeTestResults() |
0 commit comments