-
Notifications
You must be signed in to change notification settings - Fork 1
/
parseHTML.py
56 lines (48 loc) · 2.12 KB
/
parseHTML.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
from bs4 import BeautifulSoup
import re
#Try to sort them by quarter taken
def getSchedule():
classes = getClasses()
schedule = dict()
for requirement in classes.keys():
for class1 in classes[requirement][0]:
quarter = class1[0:4]
#this is honestly the greatest line of code ever
#Tries to append to the specific quarter list of classes
try:
schedule[quarter].append(class1[5:])
#If the list of classes hasn't been initialized, initialize it
except:
schedule[quarter] = [class1[5:]]
return schedule;
def getClasses():
def getIndex(index):
return visible_text.index(reqList[index][0]+" "+reqList[index][1])
soup = BeautifulSoup(open('degree.htm'), 'html.parser')
categories = soup.find_all("font")
requirements = dict()
reqList = list()
#Looking for things like 1) CSE REQ or maybe > 2) Statistics - Reqd
valid = re.compile(r".*([1-9]+\))\s([^-]+).*$")
for req in categories:
if req.string is not None:
regexResult = valid.match(req.string)
if regexResult is not None:
reqList.append(regexResult.groups())
[s.extract() for s in soup(['style', 'script', '[document]', 'head', 'title'])]
visible_text = soup.getText()
index = 0
takenCourses = re.compile(r"((?:FA|SP|WI)(?:15|16|17))\s([A-Z]{3,4}\s+[0-9]{1,3}[A-Z]*)")
missingReq = re.compile(r"\nNeeds:\s+([1-9]+)\sCourses\n")
for index in range(1,len(reqList)):
#Going to have the requirements be (takenCourses, missingCourses)
requirements[reqList[index-1][1]] = [[],0]
stringSegment = visible_text[getIndex(index-1):getIndex(index)]
regexResult = takenCourses.findall(stringSegment)
for result in regexResult:
#Add to the taken courses
requirements[reqList[index-1][1]][0].append(result[0] + " " + result[1])
regexResult = missingReq.search(stringSegment)
if regexResult is not None:
requirements[reqList[index-1][1]][1] = int(regexResult.groups()[0])
return requirements