-
Notifications
You must be signed in to change notification settings - Fork 1
/
course_scraper.py
213 lines (174 loc) · 7.09 KB
/
course_scraper.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import requests
from bs4 import BeautifulSoup
import pandas as pd
from typing import List, Tuple
FILE_NAME: str = "courses.csv"
MATRIX_FILE_NAME: str = "catalog_matrix.csv"
BASE_COURSE_CATALOG_URL: str = "https://catalog.calpoly.edu/coursesaz/"
BASE_COLLEGES_DEPT_URL: str = "https://catalog.calpoly.edu/coursesaz/#courseprefixestext"
def getTextWithinParentheses(text: str):
return text[text.find('(')+1:text.find(')')]
def clean_link_text(text):
return text.replace(')', "").replace('(', "").strip()
def parse_college_html(college_html) -> List[Tuple[str, List[str]]]:
depts = []
college_dept_pairs = []
current_college = None
for child in college_html.children:
tag = child.name
text = child.text
# dept
if(tag == 'a'):
depts.append(clean_link_text(text))
# college
elif(tag == 'strong'):
if(current_college is not None):
college_dept_pairs.append([current_college, depts])
depts = []
current_college = text.strip()
college_dept_pairs.append((current_college, depts))
return college_dept_pairs
def parse_college_department_html(html) -> List[str]:
depts = []
for dept_html in html.children:
if(dept_html.name is not None):
dept = dept_html.text.split('(')[0].strip()
prefixes = [a.text.replace(')', "")
for a in dept_html.find_all("a")]
depts.append((dept, prefixes))
return depts
# iterates through all depts of a college
# {
# 'Biological Science': [BIO, BOT MCRO, MSCI],
# 'Chemistry and Biochemisty': [CHEM]
# ...
# }
def create_depts_dict(depts):
d = {}
for dept in depts:
d |= {
dept[0]: dept[1]
}
return d
def scrape_course_prefixes():
DEPT_PREFIXES_ID: str = "courseprefixestextcontainer"
course_page = requests.get(BASE_COLLEGES_DEPT_URL)
soup = soup = BeautifulSoup(course_page.content, "html.parser")
colleges_html = soup.find(id=DEPT_PREFIXES_ID).div
college_dept_dict = {}
current_college = None
depts = []
for child in colleges_html.children:
tag = child.name
if (tag == "ul"): # nested departments
depts = parse_college_department_html(child)
elif (tag == 'p'): # college
college_dept_pairs = parse_college_html(child)
# more than one, so add all but the last to dictionary
if(len(college_dept_pairs) > 1):
# add current college and departments
college_dept_dict |= {
current_college[0]: {
current_college[0]: current_college[1]
}
}
college_dept_dict[current_college[0]
] |= create_depts_dict(depts)
# add all other colleges except for the last one
for i in range(len(college_dept_pairs)-1):
# add colleges with no departments
college_dept_dict |= {
college_dept_pairs[i][0]: {
college_dept_pairs[i][0]: college_dept_pairs[i][1]
}
}
# set current college to last in pairs
current_college = college_dept_pairs[-1]
else:
if (current_college is not None):
# add current college and departments
college_dept_dict |= {
current_college[0]: {
current_college[0]: current_college[1]
}
}
# add college departments
college_dept_dict[current_college[0]
] |= create_depts_dict(depts)
# set new current college
current_college = college_dept_pairs[0]
# add last current college
college_dept_dict |= {
current_college[0]: {
current_college[0]: current_college[1]
}
}
# add college departments
college_dept_dict[current_college[0]
] |= create_depts_dict(depts)
return college_dept_dict
def extract_course_info(data, college, dept, prefix):
prefix = prefix.lower()
url = f'{BASE_COURSE_CATALOG_URL}/{prefix}'
page = requests.get(url)
# scrape data
soup = BeautifulSoup(page.content, "html.parser")
courses = soup.find_all("div", class_="courseblock")
if (college == dept):
dept = f'{dept} Dept'
for c in courses:
course_name: List[str] = c.find(
"p", class_="courseblocktitle").strong.contents[0].split(".")
course_num: str = course_name[0].replace(
"\xa0", "-").strip() # replace nonbreaking space
name: str = course_name[1].strip()
units: str = c.find("span", class_="courseblockhours").text.strip()
description: str = c.find(
"div", class_="courseblockdesc").p.text.strip()
data.append([college, dept, course_num,
name, units, description, college+dept, dept+course_num])
def scrape_courses(prefixes_dict):
data = []
for college in prefixes_dict.keys():
for dept in prefixes_dict[college]:
for prefix in prefixes_dict[college][dept]:
if(prefix):
extract_course_info(data, college, dept, prefix)
return data
def build_df(data):
column_names = ["College", "Dept", "Course Prefix",
"Course Name", "Units", "Description", "College+Dept", "Dept+CourseNum"]
df = pd.DataFrame(data, columns=column_names)
return df
def find_match(course_list, matrix):
for row in matrix.index:
for col in matrix.columns:
match = not(course_list[(course_list['College+Dept'] == row+col)
].empty) or not(course_list[(course_list['Dept+CourseNum'] == row+col)].empty)
if(match):
matrix.loc[row, col] = 1
# print(row, col)
return
def build_adj_matrix(course_list: pd.DataFrame):
colleges = list(course_list["College"].unique())
depts = list(course_list["Dept"].unique())
courses = list(course_list["Course Prefix"].unique())
indices = [(1, college) for college in colleges] + \
[(2, dept) for dept in depts] + [(3, course) for course in courses]
multi_index = pd.MultiIndex.from_tuples(indices)
adj_matrix = pd.DataFrame(index=multi_index, columns=multi_index).fillna(0)
# grab necessary sections
colleges_to_depts = adj_matrix.loc[1, 2]
depts_to_courses = adj_matrix.loc[2, 3]
# mark matches
find_match(course_list, colleges_to_depts)
find_match(course_list, depts_to_courses)
return adj_matrix
if __name__ == "__main__":
prefixes_dict = scrape_course_prefixes()
courses = scrape_courses(prefixes_dict)
course_list = build_df(courses)
course_list.to_csv(FILE_NAME, index=False)
# course_list = pd.read_csv(FILE_NAME)
# adj_matrix = build_adj_matrix(course_list)
# adj_matrix.to_csv(MATRIX_FILE_NAME)