-
Notifications
You must be signed in to change notification settings - Fork 0
/
spider.py
171 lines (144 loc) · 4.97 KB
/
spider.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
'''
This project will gets all in person courses in UW, 2020 Fall.
Output to data.json / data.csv with each course's name, SLN, credit, and type.
Malicious and/or commercial uses are forbidden.
Yuan 2020/07
'''
import requests
from bs4 import BeautifulSoup
import re
import json
import csv
URL_ROOT = "https://www.washington.edu/students/timeschd/AUT2020/"
COURSE_NUM_LIMIT = 499
JSON_PATH = 'data.json'
CSV_PATH = 'data.csv'
'''
Returns a list of links' suffix to each departments' available courses
e.g. math.html
'''
def get_links():
html = requests.get(url=URL_ROOT).text
soup = BeautifulSoup(html, features='lxml')
hrefs = soup.find_all('a', attrs={'href': re.compile('^[a-z]*\.html')})
links = []
for l in hrefs:
links.append(l['href'])
return links
'''
Gets all in person courses's name with corresponding section numbers
whose levels are below @COURSE_NUM_LIMIT. Also get the courses' corresponding
credit and category.
@end: String, the department from which courses are fetched, e.g. 'math.html'
@return: a dictionary with keys='{course_name} {credit} {type}', values
as a list of section numbers.
'''
def get_course(end):
#Get soups
url = URL_ROOT + end
html = requests.get(url=url).text
soup = BeautifulSoup(html, features='lxml')
html2 = requests.get('http://www.washington.edu/students/crscat/' + end).text
credit_soup = BeautifulSoup(html2, features='lxml')
courses = soup.find_all('table', attrs={'width': '100%'})
in_person = {}
i = 2 #ignore previous blocks
course_name = ""
while (i < len(courses)):
course = courses[i]
if ('bgcolor' in course.attrs and course['bgcolor']=='#ffcccc'): # if it is course_name block
course_name = course.find('a', attrs={'name': re.compile('.*')})['name']
num = int(course_name[len(course_name)-3:len(course_name)])
print('current course: ' + course_name)
if (num > COURSE_NUM_LIMIT): # ignore remaining courses which are above COURSE_NUM_LIMIT
break
else: # get credit and type, integrate into course name
address = course.find('a', attrs={'href': re.compile('/students/crscat/.*')})['href']
crdt = get_credit(credit_soup, course_name)
type = get_type(course)
course_name += ' ' + crdt + ' ' + type
i += 1
else: # section block
if (check_in_person(course)):
course_num = course.find('a').string
if (course_name not in in_person): # add to result
in_person[course_name] = []
in_person[course_name].append(course_num)
i += 1
return in_person
'''
Checks if the given @course (as a soup element) is in person.
Return true if in person.
'''
def check_in_person(course):
if (course.find('a', attrs={'href': re.compile('.*maps.*')}) is None):
return False
contents = course.find('pre').contents
if ('VIA REMOTE' in contents[2]):
return False
if (len(contents) > 4 and 'VIA REMOTE' in contents[4]):
return False
return True
'''
Get the credit of the given course @name from the given @soup
@return 0 if no course was found.
@soup: the department's credit page's soup
@name: String, course name
'''
def get_credit(soup, name):
tag = soup.find('a', attrs={'name': name})
if (tag is None):
return '0'
full_title = tag.find('b').string
tpl = re.findall('\(\d.*\)', full_title)
if (tpl is not None and len(tpl) > 0):
return tpl[0][1]
else:
return '0'
'''
Get the course's category from the given @course
@course: soup's course title element
'''
def get_type(course):
b = course.find_all('b')
if (b is None or len(b) < 2):
return 'N/A'
else:
type = b[1].string
if (type is None):
return 'N/A'
return type[1:len(type)-1]
'''
Writes the given dictionary to JSON_PATH
'''
def write_to_json(in_person):
with open(JSON_PATH, 'w') as fp:
json.dump(in_person, fp, sort_keys=True, indent=4)
'''
Writes the given dictionary to CSV_PATH
'''
def write_to_csv(in_person):
with open(CSV_PATH, 'w', newline='') as csvfile:
fieldnames = ['course_num', 'credits', 'type', 'SLN']
writer = csv.writer(csvfile)
writer.writerow(fieldnames)
for key in in_person.keys():
values = in_person[key]
arr = key.split()
arr.append('0')
for value in values:
arr[3] = value
writer.writerow(arr)
'''
Runs the program, get all links and fetch all in person courses,
output to json and csv
'''
def main():
links = get_links()
in_person = {}
for link in links:
new_in_person = get_course(link)
in_person.update(new_in_person)
write_to_json(in_person)
write_to_csv(in_person)
main()