forked from andegna/convert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crowler.py
107 lines (88 loc) · 2.19 KB
/
crowler.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
from HTMLParser import HTMLParser
import math
# the list of courses
courses = []
# the tmp course
course = None
class Parser(HTMLParser):
def handle_starttag(self, tag, attrs):
# select tr.ob_gR or tr.ob_gRA or tr.ob_gRS
if tag == 'tr' and len(attrs) == 1 and attrs[0][0] == 'class' and ( attrs[0][1] == 'ob_gRA' or attrs[0][1] == 'ob_gRS' or attrs[0][1] == 'ob_gR'):
global courses, course
# tmp course loaded
if course != None and len(course) > 0:
courses.append(course)
# clear the tmp course
course = []
def handle_data(self, data):
global course
# trim the whitespace
val = data.strip()
# tmp course create and aleady has data not more than 5 element
if course != None and len(val) > 0 and len(course) < 5:
course.append(val)
def getOldVal(grade):
if grade == 'A+':
return 4.0
elif grade == 'A':
return 3.75
elif grade == 'A-':
return 3.5
elif grade == 'B+':
return 3.25
elif grade == 'B':
return 3.0
elif grade == 'C+':
return 2.75
elif grade == 'C':
return 2.5
elif grade == 'D':
return 2.25
elif grade == 'E':
return 2.0
else :
return 0.0
def getNewVal(grade):
if grade == 'A+' or grade == 'A' or grade == 'A-':
return 4.0
elif grade == 'B+' or grade == 'B':
return 3.5
elif grade == 'C+':
return 3.0
elif grade == 'C':
return 2.75
elif grade == 'D':
return 2.5
elif grade == 'E':
return 2.0
elif grade == 'F':
return 1.0
else :
return 0.0
def process():
global courses, course
totalCH = 0
totalOldCP = 0
totalNewCP = 0
lastCode = ''
for course in courses:
if len(course) == 5 and lastCode != course[0]:
# print course
totalCH = totalCH + int(course[2])
totalOldCP = totalOldCP + (int(course[2]) * getOldVal(course[3].strip()))
totalNewCP = totalNewCP + (int(course[2]) * getNewVal(course[3].strip()))
lastCode = course[0]
print 'Old GPA: ',math.floor((totalOldCP / totalCH) * 100) / 100
print 'New GPA: ',math.floor((totalNewCP / totalCH) * 100) / 100
def main():
parser = Parser()
f = open("My Grades.htm")
if f.mode == 'r':
contents = f.read()
parser.feed(contents.replace('&','x'))
else :
print 'unable to read'
courses.append(course)
process()
if __name__ == '__main__':
main()