-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtmlselector.py
244 lines (211 loc) · 8.28 KB
/
htmlselector.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
# VERSION: 0.3
# AUTHORS: imDMG [imdmgg@gmail.com]
# html selector for default python HTMLParser
import time
import re
# import logging
from html.parser import HTMLParser
start_time = time.time()
class HTMLSelector(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.pending = []
self.data = {}
self.mark = False
self.index = None
self.name = None
self.attrs = None
self.callback = None
self.container_tag = None
self.container_pair = None
self.container_group = False
self.grouping = []
self.index_count = 0
self.count = 0
def handle_starttag(self, tag, attrs):
if self.pending:
for item in self.pending:
if tag == item['tag'] and self._filter(item['attrs'], attrs):
if item['container']:
self.mark = False
self.attrs = None
self.container_tag = tag
self.container_pair = item["pair"]
return
if not self.container_tag and item['pair']:
self.mark = False
self.attrs = None
return
if self.container_tag and self.container_pair != item["pair"]:
self.mark = False
self.attrs = None
return
self.mark = True
self.attrs = attrs
self.name = item["name"]
self.callback = item["callback"]
self.container_group = item["group"]
if item['index']:
# TODO make checking for int
self.mark = False
if item['index'].find(":"):
start, end = item['index'].split(":")
if self.index_count in range(int(start), int(end) + 1):
self.mark = True
else:
if self.index_count == int(item['index']):
self.mark = True
self.index_count += 1
def handle_endtag(self, tag):
if tag == self.container_tag:
if self.container_group:
self.data[self.name].append(self.grouping)
self.grouping = []
self.index_count = 0
self.container_tag = None
self.container_pair = None
def handle_data(self, data):
if self.mark:
if callable(self.callback):
self.attrs, data = self.callback(self.attrs, data)
self.callback = None
# ---
if not self.data.get(self.name):
self.data[self.name] = []
if self.container_group:
self.grouping.append({"attrs": dict(self.attrs), "data": data})
else:
self.data[self.name].append({"attrs": dict(self.attrs), "data": data})
self.mark = False
self.attrs = None
def error(self, message):
pass
def find(self, *args):
self._selector_re(*args)
return self
# def where(self, method, *args):
# getattr(self, method)(*args)
# pass
#
# def container(self, *args):
# pass
#
# def content(self, *args):
# pass
#
# def group(self, trigger=True):
# self.container_group = trigger
# return self
def _selector(self, cond: str, storage="latest"):
container = False
elems = cond.split(" ")
for n, elem in enumerate(elems):
tag = elem
index = ""
dictattrs = {}
if "(" in elem:
index = elem[elem.index("(") + 1:elem.index(")")]
tag = elem[0:elem.index("(")]
if "[" in elem:
tag = elem[0:elem.index("[")]
attrs = elem[elem.index("[") + 1:elem.index("]")].split(";")
for attr in attrs:
name, value = attr.split("=")
if name == "class":
# or
if "|" in value:
value = tuple(value.split("|"))
# and
if "." in value:
value = value.split(".")
dictattrs.update({name: value})
if len(elems) > 1 and n < len(elems) - 1:
container = True
content = True if len(elems) > 1 and n == len(elems) - 1 else False
self.pending.append({"tag": tag,
"attrs": dictattrs,
"container": container,
"content": content,
"index": index})
container = False
# container = tag
self.name = storage
return self
def _selector_re(self, expression: str, store_name="latest", group=False, callback=None):
pair = None
elems = expression.split(" ")
for n, elem in enumerate(elems):
# TODO make link with container and content
tag = re.search(r'(\w+)[\[(]?', elem)[1]
attrs = dict(re.findall(r'(\w+)=([\w|.]+)', elem))
index = re.search(r'\(([\d:]+)\)', elem)
if attrs.get("class"):
# or
if "|" in attrs["class"]:
attrs["class"] = tuple(attrs["class"].split("|"))
# and
elif "." in attrs["class"]:
attrs["class"] = attrs["class"].split(".")
container = None
if len(elems) > 1:
container = False if n > 0 else True
if n == 0:
pair = elem
# content = True if len(elems) > 1 and n == len(elems) - 1 else False
self.pending.append({"tag": tag,
"name": store_name,
"attrs": attrs,
"container": container,
"pair": pair,
"group": group,
"index": index[1] if index else "",
"callback": callback})
return self
def _data_pass(self, tag, attrs):
pass
def _filter(self, what, where):
"""
:type what: dict
:param where:
:return:
"""
if not what:
return True
cond = []
# find the attr and value match
# TODO rebuild loop up side down
for name, value in dict(where).items():
if name in what.keys():
# TODO make direct match
# attr class with multiple values
if type(what[name]) == tuple:
cond.append(any([x in value for x in what[name]]))
continue
if type(what[name]) == list:
cond.append(all([x in value for x in what[name]]))
continue
cond.append(what[name] in value)
return all(cond if len(cond) else [False])
if __name__ == "__main__":
f = open("result.html", "r")
parser = HTMLSelector()
# None: we not store the data, just mark position
# find(tr.bg td.s|sl_s|slp)
# parser.find("div[class=bx1_0] td[colspan=5]", "amount", None, lambda atr, dt: [atr, dt.split(" ")[1]])
parser.find("tr[class=bg] a[class=r0|r1]", "torrents", True)
parser.find("tr[class=bg] td(3:5)", "torrents", True)
# parser._selector_re("select[class=w190]")
# parser._selector("tr[class=bg] td(3:6)", "params").group()
print(parser.pending)
# line = "td[class=s|sl_s|slp;id=me](3:5)"
# pair = re.findall(r"[\w]+=[\w|]+", line)
# print(re.match(r"^.*(\w+)=([\w|]+)", line).groups())
# parser.find('tr', None, {"class": "bg"}).\
# content('td', "td", {"class": "s"}).group("tr")
# a.r0|r1
# parser.find('img', "torrent_link", {"class": "block"})
# print(parser.pending)
parser.feed(f.read())
print(parser.data)
# print(len(parser.data["kaka"]))
print("--- %s seconds ---" % (time.time() - start_time))