-
Notifications
You must be signed in to change notification settings - Fork 7
/
Qscore_v2.py
167 lines (136 loc) · 5.35 KB
/
Qscore_v2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/python
# -*- coding: utf-8 -*-
__author__ = 'BG'
import urllib
import urllib2
import cookielib
import re
class URP:
"""docstring for URP"""
def __init__(self,username,password):
self.usr = username
self.psw = password
self.hosturl = 'http://bksjw.chd.edu.cn/'
self.posturl = 'http://bksjw.chd.edu.cn/loginAction.do'
cj = cookielib.LWPCookieJar()
cookie_support = urllib2.HTTPCookieProcessor(cj)
opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler)
urllib2.install_opener(opener)
h = urllib2.urlopen(self.hosturl)
self.headers = {'User-Agent': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729)',
'Referer':'http://bksjw.chd.edu.cn/'}
self.postData = {'dllx':'dldl','zjh':self.usr,'mm':self.psw }
self.postData = urllib.urlencode(self.postData)
self.request = urllib2.Request( self.posturl, self.postData, self.headers )
def login(self):
flag = False
try:
urllib2.urlopen(self.request)
urllib2.urlopen('http://bksjw.chd.edu.cn/gradeLnAllAction.do?type=ln&oper=qbinfo&lnxndm').read()
except urllib2.HTTPError, e:
print '-----------------------------------------------------------'
print 'Login Failed [%s], maybe your username or password is error!' %e.code
print '-----------------------------------------------------------'
else:
print '-----------------------------------------------------------'
print 'Login Successful'
print '-----------------------------------------------------------'
flag = True
return flag
# text = response.read()
def BXQ_score(self):
self.score_html = urllib2.urlopen('http://bksjw.chd.edu.cn/bxqcjcxAction.do').read()
def NJ_score(self):
self.score_html = urllib2.urlopen('http://bksjw.chd.edu.cn/gradeLnAllAction.do?type=ln&oper=qbinfo&lnxndm').read()
def TAB_score(self):
self.score_html = urllib2.urlopen('http://bksjw.chd.edu.cn/reportFiles/student/cj_zwcjd_all.jsp').read()
def FA_score(self):
self.score_html = urllib2.urlopen('http://bksjw.chd.edu.cn/gradeLnAllAction.do?type=ln&oper=fainfo&fajhh=3792').read()
def calc_gpa(self):
reg = re.compile(r'<tr class="odd".*?>.*?<td.*?</td>.*?<td.*?</td>.*?<td.*?</td>.*?<td.*?</td>.*?<td align="center">\s*(\S+)\s*</td>.*?<td.*?</td>.*?<td align="center">.*?<p align="center">(.*?) .*?</P>.*?</td>.*?<td.*?</td>.*?</tr>.*?',re.S)
myItems = reg.findall(self.score_html)
# print len(myItems)
score = []
grades = []
sum = 0.0
weight = 0.0
for item in myItems:
grades.append(item[0])
score.append(item[1])
for i in range(len(score)):
try:
we = float(grades[i])
add = float(score[i])
sum += add*we
weight += we
except:
if score[i] == "优秀":
sum += 95.0*we
weight += we
elif score[i] == "良好":
sum += 85.0*we
weight += we
elif score[i] == "中等":
sum += 75.0*we
weight += we
elif score[i] == "及格":
sum += 60.0*we
weight += we
else:
weight += we
if weight == 0 :
return
print 'your GPA is ', sum/weight
def save_html(self):
fout=open("score.html","w")
head = '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
self.score_html = head + self.score_html.decode('GBK').encode('UTF-8')
fout.write(self.score_html)
print '-----------------------------------------------------------'
print 'The result was saved in score.html\nGod bless you!!!!!!'
def save_img(self):
pic = urllib2.urlopen('http://bksjw.chd.edu.cn/xjInfoAction.do?oper=img').read()
open("zjz.jpg","wb").write(pic)
print '-----------------------------------------------------------'
print 'Your pic is saved in zjz.jpg!'
def query_score():
print 'Please input your username:'
usr = raw_input()
print 'Please input your password:'
psw = raw_input()
urp = URP(usr,psw)
if urp.login() == True:
print 'Please choose an mode:'
print '1 - your score of each subject in this term'
print '2 - all of your score in each term'
print '3 - all of your score in a table'
print '4 - all of your score by plan'
choose = raw_input()
if choose == '1':
urp.BXQ_score()
elif choose == '2':
urp.NJ_score()
elif choose == '3':
urp.TAB_score()
else:
urp.FA_score()
urp.save_html()
urp.calc_gpa()
urp.save_img()
else:
return
'''
<tr class="odd" onMouseOut="this.className='even';" onMouseOver="this.className='evenfocus';">
<td align="center"> 1107001S </td> 课程号
<td align="center"> 123 </td> 课序号
<td align="center"> 形势与政策 </td> 课程名
<td align="center"> </td> 英文课程名
<td align="center"> 2 </td> 学分
<td align="center"> 公共基础必修 </td> 课程属性
<td align="center"> <p align="center">92.0 </P> </td> 成绩
<td> <p align="center"> </P> </td> 未通过原因
</tr>
'''
if __name__ == '__main__':
query_score()
raw_input ('Press Enter to exit!')