-
Notifications
You must be signed in to change notification settings - Fork 0
/
myparser.py
258 lines (212 loc) · 7.24 KB
/
myparser.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
from html.parser import HTMLParser
class MyHTMLParser(HTMLParser):
def error(self, message):
pass
def __init__(self, *args, **kwargs):
super(self.__class__, self).__init__()
self.path = []
self.data = []
self.valid_tags = []
self.skip_tags = []
self.parsers = {}
self.current_tag = None
self.is_current_tag_valid = False
for arg in args:
for key in arg:
setattr(self, key, arg[key])
# print("Parsing of tags:", self.valid_tags)
def handle_starttag(self, tag, attrs):
if tag == 'br':
return
self.path.append(tag)
self.is_current_tag_valid = self.valid(tag)
self.current_tag = tag
if not self.is_current_tag_valid:
return
if self.is_skip(tag):
return
self.data.append((tag, attrs))
super(self.__class__, self).handle_starttag(tag, attrs)
def handle_endtag(self, tag):
if tag == 'br':
return
i = len(self.path) - 1 - self.path[::-1].index(tag)
self.path = self.path[:i]
self.current_tag = self.path[-1] if self.path else None
self.is_current_tag_valid = self.valid(self.current_tag)
if not self.is_current_tag_valid:
return
super(self.__class__, self).handle_endtag(tag)
def handle_data(self, data):
if not self.current_tag or not self.is_current_tag_valid:
return
if not self.parsers:
self.default_parser(data)
else:
if not self.parsers[self.current_tag]:
self.default_parser(data)
else:
self.parsers[self.current_tag](data, self)
super(self.__class__, self).handle_data(data)
def default_parser(self, data):
if self.data:
idx = len(self.data) - 1
last = self.data[idx]
if len(last) > 2:
self.data[idx] += (data,)
else:
self.data[idx] = (last[0], last[1], data)
def valid(self, tag):
if self.valid_tags:
return True if tag in self.valid_tags else False
return True
def is_skip(self, tag):
if len(self.skip_tags) > 0:
return True if tag in self.skip_tags else False
return False
def feed_and_return(self, data):
self.feed(data)
return self
class AnektodHTMLParser(HTMLParser):
def error(self, message):
pass
def __init__(self, *args, **kwargs):
super(self.__class__, self).__init__(*args, **kwargs)
self.ready = []
self.lines = ""
self.collecting = False
self.begin = False
self.internal_divs = []
def handle_starttag(self, tag, attrs):
if tag not in ['div', 'p']:
return
for attr in attrs:
if 'anekdot' in attr:
if not self.collecting:
self.collecting = True
else:
self.internal_divs.append('div')
return
if tag == 'p':
self.begin = True
def handle_data(self, data):
if self.collecting:
if data:
self.lines += data
def handle_endtag(self, tag):
if tag not in ['div', 'p']:
return
if tag == 'div':
if self.internal_divs:
self.internal_divs.pop('div')
if not self.internal_divs:
self.collecting = False
return
if tag == 'p':
self.begin = False
if self.lines.replace('\n', '').replace('\r', ''):
self.ready.append(self.lines)
self.lines = ""
class LinksHTMLParser(HTMLParser):
def error(self, message):
pass
def __init__(self, *args, **kwargs):
super(self.__class__, self).__init__(*args, **kwargs)
self.ready = []
self.lines = ""
self.collecting = False
self.begin = False
self.internal_divs = []
self.media_heading = False
self.links = []
self.info_add = False
self.info = []
self.info_buffer = []
def handle_starttag(self, tag, attrs):
if tag not in ['p', 'h4', 'a']:
return
for attr in attrs:
if 'media-heading' in attr:
self.media_heading = True
if 'link-reverse' in attr:
self.info_add = True
if tag == 'a' and self.media_heading:
self.collecting = True
self.links.append(attr[1])
if tag == 'p':
self.begin = True
def handle_data(self, data):
if self.collecting:
if data:
self.lines += data
if self.info_add:
self.info_buffer.append(data)
def handle_endtag(self, tag):
if tag not in ['p', 'h4', 'a']:
return
if tag == 'a' and self.media_heading:
self.collecting = False
if tag == 'h4':
self.media_heading = False
self.collecting = False
if self.lines.replace('\n', '').replace('\r', '').strip():
self.ready.append(self.lines.replace('\n', '').replace('\r', '').strip())
self.lines = ""
if tag == 'p' and self.info_add:
self.info_add = False
self.info.append(' '.join(self.info_buffer))
self.info_buffer = []
class StoryHTMLParser(HTMLParser):
def error(self, message):
pass
def __init__(self, *args, **kwargs):
super(self.__class__, self).__init__(*args, **kwargs)
self.ready = []
self.lines = ""
self.collecting = False
self.begin = False
self.internal_divs = []
self.pages = []
self.page_buffer = ''
self.pages_collect = False
def handle_starttag(self, tag, attrs):
if tag not in ['div', 'p', 'ul', 'a']:
return
for attr in attrs:
if 'full_text' in attr:
if not self.collecting:
self.collecting = True
else:
self.internal_divs.append('div')
return
if tag == 'p':
self.begin = True
if tag == 'ul':
for attr in attrs:
if 'pagination' in attr:
self.pages_collect = True
if tag == 'a' and self.pages_collect:
self.page_buffer = attrs[0][1]
def handle_data(self, data):
if self.collecting:
if data:
self.lines += data
def handle_endtag(self, tag):
if tag not in ['div', 'p', 'ul', 'a']:
return
if tag == 'div':
if self.internal_divs:
self.internal_divs.pop('div')
if not self.internal_divs:
self.collecting = False
return
if tag == 'p':
self.begin = False
if self.lines.replace('\n', '').replace('\r', ''):
self.ready.append(self.lines)
self.lines = ""
if tag == 'ul' and self.pages_collect:
self.pages_collect = False
if tag == 'a' and self.pages_collect:
self.pages.append(self.page_buffer)
self.page_buffer = ''