forked from SMKIDRaadet/dtu-course-analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.py
239 lines (200 loc) · 7.56 KB
/
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# -*- coding: utf8 -*-
import json
import time
start_time = time.time()
import requests
import re
import pickle
import pprint
from bs4 import BeautifulSoup#
import json
import sys
import datetime
import traceback
from tqdm import tqdm
now = datetime.datetime.now()
pp = pprint.PrettyPrinter(indent=2)
file = open("coursenumbers.txt", 'r')
courses=file.read().split(",")
file.close()
file = open("secret.txt", 'r')
key=file.read()
file.close()
def printlog(txt):
f = open('out.txt','w')
print(txt)
f.write(txt+'\n') # python will convert \n to os.linesep
def printTime():
printlog("--- %s seconds ---" % (time.time() - start_time))
def extractstr(pre, post, body):
m = re.search(pre + '(.+?)' + post, body)
if m:
return m.group(1)
else:
return ""
def extractURLs(pre, post, body):
try:
body = " ".join(body.split())
body = body.split(pre, 1)[1]
body = body.split(post, 1)[0]
return extractlinks(body)
except Exception:
return ""
def extractlinks(html):
soup = BeautifulSoup(html,"html.parser")
anchors = soup.findAll('a')
links = []
for a in anchors:
links.append(a['href'])
return links
reqC=0
def respObj(url):
global reqC
reqC += 1
r = requests.get(url, cookies={'ASP.NET_SessionId' : key})
if r.status_code==200:
return r.text
else:
return False
respObj("http://karakterer.dtu.dk/Histogram/1/02110/Winter-2016")
def RepresentsInt(s):
try:
int(s)
return True
except ValueError:
return False
def gradeParticipants(html):
soup = BeautifulSoup(html, 'html.parser')
return int(removeWhitespace(soup.find_all('table')[0].find_all('tr')[1].find_all('td')[1].text))
def removeWhitespace(txt):
txt = " ".join(txt.split())
txt = txt.replace(" ", "")
return txt
gradeHTMLNames=["Ej mødt", "Syg", "Bestået", "Ikke bestået", "-3", "00", "02", "4", "7", "10", "12"]
grades=["absent", "sick", "p", "np", "-3", "00", "02", "4", "7", "10", "12"]
class Course(object):
def __init__(self, courseN):
self.courseN = courseN
self.initDic()
self.reviewLinks = []
self.gradeLinks = []
def initDic(self):
self.dic= {}
for grade in grades:
self.dic[grade] = 0
def setHTML(self, HTML):
self.HTML=HTML
self.soup=BeautifulSoup(HTML, 'html.parser')
def extractParticipants(self):
return self.participants
def extractGrades(self, url):
try:
html = respObj(url)
if html:
soup = BeautifulSoup(html, 'html.parser')
obj = soup.find_all('table')[2].find_all('tr')
dic={};
for i in range(1, len(obj)):
name = removeWhitespace( obj[i].find_all('td')[0].text )
val = removeWhitespace( obj[i].find_all('td')[1].text )
dic[name]=val
#pp.pprint(dic);
timestamp = url.split("/")[-1]
season = timestamp.split("-")[0]
year = timestamp.split("-")[-1]
if( int(year) > now.year+2):
year = str(int(year)-100)
timestamp=season + "-" + year
dic["timestamp"] = timestamp
participants = int(removeWhitespace(soup.find_all('table')[0].find_all('tr')[1].find_all('td')[1].text))
dic["participants"] = participants
pass_percentage = int(removeWhitespace(soup.find_all('table')[0].find_all('tr')[2].findChildren()[1].text.split("(")[1].split("%")[0]))
dic["pass_percentage"] = pass_percentage
try:
avg = float(removeWhitespace(soup.find_all('table')[0].find_all('tr')[3].findChildren()[1].text.split(" (")[0]).replace(",", "."))
dic["avg"] = avg
except Exception:
traceback.format_exc()
return dic;
return False;
except IndexError as e:
return False
def extractReviews(self, url):
#url="https://evaluering.dtu.dk/kursus/01005/122714"
dic = {}
try:
html = respObj(url)
if html:
soup = BeautifulSoup(html, 'html.parser')
containers = soup.findAll("div", {"class": "ResultCourseModelWrapper"})#[2].find_all('tr')
publicContainer = soup.find("div", {"id": "CourseResultsPublicContainer"})
dic["participants"]=int(publicContainer.find("table").findAll("tr")[1].findAll("td")[0].text)
dic["timestamp"] = publicContainer.find("h2").text[-3:]
for container in containers:
name = container.find("div", {"class": "FinalEvaluation_Result_QuestionPositionColumn"}).text
name = removeWhitespace(name)
dic[name] = {}
dic[name]["question"] = container.find("div", {"class": "FinalEvaluation_QuestionText"}).text
for i, row in enumerate(container.findAll("div", {"class": "RowWrapper"})):
dic[name][i] = removeWhitespace(row.find("div", {"class": "FinalEvaluation_Result_AnswerCountColumn"}).find("span").text)
return dic
except KeyError:
return False
def gather(self):
dic = {}
d = [["grades", self.gradeLinks, self.extractGrades], ["reviews", self.reviewLinks, self.extractReviews]]
foundData = False
for lst in d:
crawl = self.crawl(lst[0], lst[1], lst[2])
if(crawl):
dic[lst[0]] = crawl
foundData = True
if(foundData):
return dic
else:
return False
def crawl(self, name, URLs, f):
lst = []
foundData = False
for i, link in enumerate(URLs):
data = f(link)
if(data):
foundData = True
data["url"] = link
lst.append(data)
if(foundData):
return lst
else:
return False
def __str__(self):
return "Course: %s" % (self.courseN)
courseDic = {}
for i, courseN in tqdm(enumerate(courses),total=len(courses)):
try:
#print("Course: "+courseN)
course = Course(courseN)
overviewResp = respObj("http://kurser.dtu.dk/course/" + courseN + "/info")
if overviewResp:
soup = BeautifulSoup(overviewResp, "html.parser")
links = soup.find_all('a')
for link in links:
l=link.get('href')
if "evaluering" in l:
course.reviewLinks.append(l)
elif "karakterer" in l:
course.gradeLinks.append(l)
crawl = course.gather()
if(crawl):
courseDic[courseN] = crawl
soup = BeautifulSoup(respObj("http://kurser.dtu.dk/course/" + courseN), "html.parser")
courseName = soup.find_all('h2')[0].contents[0].split(" ", 1)[1]
courseDic[courseN]["name"] = courseName
except KeyboardInterrupt:
break
except Exception as e:
printlog(str(e))
printlog("Skipping " + str(courseN))
with open('coursedic.json', 'w') as outfile:
json.dump(courseDic, outfile)
outfile.close()
printlog("Requests sent: " + str(reqC))