-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrereqScraper.py
More file actions
71 lines (58 loc) · 2.03 KB
/
Copy pathPrereqScraper.py
File metadata and controls
71 lines (58 loc) · 2.03 KB
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
from bs4 import BeautifulSoup
from classes import Requirement, Course
import requests
import unicodedata
def getPrereqs(course):
# Get the html data in raw format
r = requests.get("https://act.ucsd.edu/scheduleOfClasses/scheduleOfClassesPreReq.htm?termCode=FA19&courseId=" +
str(course.department) + str(course.code))
data = r.text
soup = BeautifulSoup(data, features="html.parser")
# Create an array of elements from the table data, deleting the first row
table = soup.findAll('span')
elements = []
for i in table:
elements.append(i.get_text().strip())
if(len(elements) == 0):
return []
del elements[0]
# If there is only one prereq, add it and return
rawReqs = []
if(len(elements) == 1):
rawReqs.append([])
rawReqs[0].append(elements[0])
return cleanRawReqs(rawReqs)
# Populate the prereqs data array
# List of lists of classes that fulfil the same prereq
i = 0
j = -1
while(i < len(elements) - 1):
# Use the "or" in the table to differentiate between classes fulfilling the same prereq
rawReqs.append([])
j += 1
rawReqs[j].append(elements[i])
i += 1
while(i < len(elements) - 1 and elements[i].find("or") > -1):
rawReqs[j].append(elements[i + 1])
i += 2
#print(rawReqs)
return cleanRawReqs(rawReqs)
# Takes a list of requirements of class strings, and turns them into a list of requirements with course instances
def cleanRawReqs(rawReqs):
print(str(rawReqs))
requirements = []
for reqs in rawReqs:
requirement = Requirement([])
for rawCourse in reqs:
splitClassArray = splitClass(rawCourse)
course = Course(splitClassArray[0], splitClassArray[1])
requirement.courses.append(course)
requirements.append(requirement)
return requirements
def splitClass(classString):
# Takes in a class string (e.g "CSE21") and outputs an array ["Department", "Code"]
for i, c in enumerate(classString):
if c.isdigit():
#print([classString[:i], classString[i:]])
return [classString[:i], classString[i:]]
print(prereqs)