Repository navigation
Expand file tree
/
Copy pathCustomParser.py
More file actions
71 lines (59 loc) · 2.66 KB
/
Copy pathCustomParser.py
File metadata and controls
71 lines (59 loc) · 2.66 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
import re
from enum import Enum
from html.parser import HTMLParser
class _GymListParserState(Enum):
INIT = 0
ID = 1
NAME = 2
class GymListParser(HTMLParser):
def __init__(self, *, convert_charrefs=True):
super().__init__(convert_charrefs=convert_charrefs)
self.infos = list()
self.__state = _GymListParserState.INIT
def handle_starttag(self, tag, attrs):
if tag == 'td' and ('class', 'feld') in attrs:
if self.__state == _GymListParserState.INIT:
self.infos.append(list())
self.__state = _GymListParserState.ID
def handle_data(self, data):
if self.__state == _GymListParserState.ID:
self.infos[len(self.infos) - 1].append(data)
self.__state = _GymListParserState.NAME
elif self.__state == _GymListParserState.NAME:
self.infos[len(self.infos) - 1].append(data)
self.__state = _GymListParserState.INIT
class _ScheduleParserState(Enum):
INIT = 0
DESCRIPTION = 1
EVENT = 2
APPOINTMENTTYPE = 3
LOCATION = 4
SECTION = 5
class ScheduleParser(HTMLParser):
def __init__(self, *, convert_charrefs=True):
super().__init__(convert_charrefs=convert_charrefs)
self.__state = _ScheduleParserState.INIT
self.schedule = list()
def handle_starttag(self, tag, attrs):
if tag == 'div' and ('class', 'tool_beschr') in attrs:
if self.__state == _ScheduleParserState.INIT:
self.schedule.append(list())
self.__state = _ScheduleParserState.DESCRIPTION
elif tag == 'br':
if self.__state == _ScheduleParserState.DESCRIPTION:
self.__state = _ScheduleParserState.EVENT
elif self.__state == _ScheduleParserState.EVENT:
self.__state = _ScheduleParserState.APPOINTMENTTYPE
elif self.__state == _ScheduleParserState.APPOINTMENTTYPE:
self.__state = _ScheduleParserState.LOCATION
elif self.__state == _ScheduleParserState.LOCATION:
self.__state = _ScheduleParserState.SECTION
elif self.__state == _ScheduleParserState.SECTION:
self.__state = _ScheduleParserState.INIT
def handle_data(self, data):
if self.__state == _ScheduleParserState.DESCRIPTION:
self.schedule[len(self.schedule) - 1].append(data.strip('\n\r\t'))
elif self.__state == _ScheduleParserState.APPOINTMENTTYPE:
self.schedule[len(self.schedule) - 1].append(data.strip('\n\r\t'))
elif self.__state == _ScheduleParserState.SECTION:
self.schedule[len(self.schedule) - 1].append(data.strip('\n\r\t'))