-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.py
82 lines (64 loc) · 2.78 KB
/
Parser.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
from HTMLParser import HTMLParser
class MyHTMLParser(HTMLParser):
starts = []
data = []
toIgnore = ['CRN', 'Course #', 'Course Title', 'Units', 'Actv', 'Days', 'Time', 'Bldg/Rm', 'Start - End', 'Instructor', 'Max Enrl', 'Act Enrl', 'Seats Avail', 'Skip to top of page', 'Search Courses']
passes = 0
def handle_starttag(self, tag, attrs):
self.starts.append(tag)
def handle_endtag(self, tag):
self.starts.pop()
def handle_data(self, data):
if len(self.starts) < 1:
return
if data in self.toIgnore:
return
if self.starts[len(self.starts) - 1] == 'h3':
if data == "&":
self.data[len(self.data)-1][0] = self.data[len(self.data)-1][0] + data
self.passes = 1
else:
if self.passes == 0:
self.data.append(["startsubject", "separator"])
self.data.append([data, "subject"])
else:
self.data[len(self.data)-1][0] = self.data[len(self.data)-1][0] + data
self.passes -= 1
if self.starts[len(self.starts) - 1] == 'a':
self.data.append(["startcourse", "separator"])
self.data.append([data, "crn"])
if self.starts[len(self.starts) - 1] == 'small':
if data == "&":
self.data[len(self.data)-1][0] = self.data[len(self.data)-1][0] + data
self.passes = 1
else:
if self.passes == 0:
self.data.append([data, "info"])
else:
self.data[len(self.data)-1][0] = self.data[len(self.data)-1][0] + data
self.passes -= 1
parser = MyHTMLParser()
from urllib import urlopen
s = urlopen("https://mystudentrecord.ucmerced.edu/pls/PROD/xhwschedule.P_ViewSchedule?validterm=202210&openclasses=N%22").read() # Extract from html file
parser.feed(s) # Parse it into data elements (subject, crn, or info)
r = open("testfiles/result.csv", "w")
r.write("CRN/Class, Course ID, Course Name, Units, Type, Days, Hours, Room, Dates, Instructor, Capacity, Enrolled, Available, Type2, Days2, Hours2, Room2, Dates2, Type3, Days3, Hours3, Room3, Dates3\n")
column = 0
for info in parser.data:
if info[1] == 'subject':
r.write("\n" + info[0])
if info[1] == 'crn':
r.write("\n" + info[0])
column = 0
if info[1] == 'info':
if column == 2:
if len(info[0]) > 1:
continue
if column == 0:
info[0] = info[0].replace(" ", "")
r.write("," + info[0].replace(",", ""))
column += 1
if info[0] == "TBD-TBD":
r.write(",")
column += 1
r.close()