-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.py
28 lines (23 loc) · 1.18 KB
/
parse.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
import os
import glob
import codecs
import lxml.html
import re
import csv
writer = csv.writer(open("exams_raw.csv", "w"))
writer2 = csv.writer(open("exams.csv", "w"))
exams_set = list()
for i, infile in enumerate(glob.glob(os.path.join("data/", "*.html"))):
print i
with codecs.open(infile, "r", "utf-8") as f: source = f.read()
html = lxml.html.fromstring(source)
if not len(html.xpath("//table[@id='UCResultsTable_resultsTbl']")):
continue
exams = html.get_element_by_id("UCResultsTable_resultsTbl").xpath("//tr[td/@class = 'results noprint']")
for exam in exams:
# details = ["<a onClick='event_add($(this))'>%s</a>" % re.sub("<br>|\n", " ", re.sub("</?td( class=\"b\")?>|</?(div|input).*>|\t", "", lxml.html.tostring(detail))) for detail in exam.xpath("td[not(input)]")]
details = [re.sub("<br>|\n", " ", re.sub("</?td( class=\"b\")?>|</?(div|input).*>|\t", "", lxml.html.tostring(detail))) for detail in exam.xpath("td[not(input)]")]
if details not in exams_set:
writer.writerow(details)
writer2.writerow(["<a onClick='event_add($(this))'>%s</a>" % detail for detail in details])
exams_set.append(details)