-
Notifications
You must be signed in to change notification settings - Fork 3
/
segment.py
367 lines (301 loc) · 12.9 KB
/
segment.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from html.parser import HTMLParser
from bs4 import BeautifulSoup
from urllib.parse import urljoin
from PIL import Image
import common
import bs4
import requests
import shutil
import setting
class Segment:
def __init__(self):
options = Options()
# options.binary_location = setting.CHROME_BINARY_LOCATION
options.add_argument('--headless')
self.browser = webdriver.Chrome(chrome_options=options, executable_path=setting.DRIVER_PATH)
self.browser.set_window_size(setting.SCREEN_WIDTH, 800) # set the window size that you need
self.parser = HTMLParser()
def segment(self, url, output_folder="output", is_output_images=False):
self.url = url
self.output_folder = self.remove_slash(output_folder)
self.log = common.log()
self.log.write("Crawl HTML Document from %s" % self.url)
self.__crawler()
self.log.write("Run Pruning on %s" % self.url)
self.__pruning()
self.log.write("Run Partial Tree Matching on %s" % self.url)
self.__partial_tree_matching()
self.log.write("Run Backtracking on %s" % self.url)
self.__backtracking()
self.log.write("Output Result JSON File on %s" % self.url)
self.__output()
if is_output_images:
self.log.write("Output Images on %s" % self.url)
self.__output_images()
self.log.write("Finished on %s" % self.url)
def __crawler(self):
self.browser.get(self.url)
self.soup = BeautifulSoup(self.browser.page_source, 'html.parser')
page_height = self.browser.find_element_by_tag_name("body").rect["height"]
self.browser.set_window_size(setting.SCREEN_WIDTH, page_height)
common.prepare_clean_dir(self.output_folder)
self.browser.save_screenshot(self.output_folder + "/screenshot.png")
def __pruning(self):
tagbody = self.soup.find("body")
tagbody["lid"] = str(-1)
tagbody["sn"] = str(1)
self.allnodes = [tagbody]
i = 0
while len(self.allnodes) > i:
children = []
for child in self.allnodes[i].children:
if isinstance(child, bs4.element.Tag):
children.append(child)
sn = len(children)
for child in children:
child["lid"] = str(i)
child["sn"] = str(sn)
self.allnodes.append(child)
i += 1
pass
def __partial_tree_matching(self):
self.blocks = []
lid_old = -2
i = 0
while i < len(self.allnodes):
node = self.allnodes[i]
if 'extracted' in node.attrs:
i += 1
continue
sn, lid = int(node["sn"]), int(node["lid"])
if lid != lid_old:
max_window_size = int(sn / 2)
lid_old = lid
for ws in range(1, max_window_size + 1):
pew, cew, new = [], [], []
for wi in range(i - ws, i + 2 * ws):
if wi >= 0 and wi < len(self.allnodes) and int(self.allnodes[wi]["lid"]) == lid:
cnode = self.allnodes[wi]
if wi >= i - ws and wi < i:
pew.append(cnode)
if wi >= i and wi < i + ws:
cew.append(cnode)
if wi >= i + ws and wi < i + 2 * ws:
new.append(cnode)
pass
isle = self.__compare_nodes(pew, cew)
isre = self.__compare_nodes(cew, new)
if isle or isre:
self.blocks.append(cew)
i += ws - 1
max_window_size = len(cew)
self.__mark_extracted(cew)
break
i += 1
pass
def __mark_extracted(self, nodes):
for node in nodes:
node["extracted"] = ""
lid = node["lid"]
parent = node
while parent.parent is not None:
parent = parent.parent
parent["extracted"] = ""
parent["sid"] = lid
nodecols = [node]
for nodecol in nodecols:
for child in nodecol.children:
if isinstance(child, bs4.element.Tag):
nodecols.append(child)
nodecol["extracted"] = ""
def __compare_nodes(self, nodes1, nodes2):
if len(nodes1) == 0 or len(nodes2) == 0:
return False
return self.__get_nodes_children_structure(nodes1) == self.__get_nodes_children_structure(nodes2)
pass
def __get_nodes_children_structure(self, nodes):
structure = ""
for node in nodes:
structure += self.__get_node_children_structure(node)
return structure
def __get_node_children_structure(self, node):
nodes = [node]
structure = ""
for node in nodes:
for child in node.children:
if isinstance(child, bs4.element.Tag):
nodes.append(child)
structure += node.name
return structure
def __backtracking(self):
for node in self.allnodes:
if (node.name != "body") and (node.parent is not None) and ('extracted' not in node.attrs) and (
'extracted' in node.parent.attrs):
self.blocks.append([node])
self.__mark_extracted([node])
pass
def __get_element(self, node):
# for XPATH we have to count only for nodes with same type!
length = 1
for previous_node in list(node.previous_siblings):
if isinstance(previous_node, bs4.element.Tag):
length += 1
if length > 1:
return '%s:nth-child(%s)' % (node.name, length)
else:
return node.name
def __get_css_selector(self, node):
path = [self.__get_element(node)]
for parent in node.parents:
if parent.name == "[document]":
break
path.insert(0, self.__get_element(parent))
return ' > '.join(path)
def __get_css_background_image_urls(self, node):
nodes = [node]
image_urls = []
structure = ""
for node in nodes:
for child in node.children:
if isinstance(child, bs4.element.Tag):
nodes.append(child)
for node in nodes:
try:
css_selector = self.__get_css_selector(node)
url = self.browser.find_element_by_css_selector(css_selector).value_of_css_property("background-image")
if url != "none":
url = url.replace('url(', '').replace(')', '').replace('\'', '').replace('\"', '')
url = urljoin(self.url, url)
image_urls.append(url)
except:
pass
return image_urls
def __get_css_selector(self, node):
path = [self.__get_element(node)]
for parent in node.parents:
if parent.name == "[document]":
break
path.insert(0, self.__get_element(parent))
return ' > '.join(path)
def __rgba2RGBA(self, rgba):
try:
rgba = rgba.replace("rgba(", "").replace(")", "")
(R, G, B, A) = tuple(rgba.split(","))
return int(R), int(G), int(B), float(A)
except:
return 0, 0, 0, 0
def __get_css_background_color(self, node):
nodes = [node]
for p in node.parents:
nodes.append(p)
(R, G, B) = (255, 255, 255)
for node in nodes:
try:
css_selector = self.__get_css_selector(node)
color = self.browser.find_element_by_css_selector(css_selector).value_of_css_property(
"background-color")
Rn, Gn, Bn, A = self.__rgba2RGBA(color)
if A == 1:
(R, G, B) = (Rn, Gn, Bn)
break
except:
pass
return R, G, B
def __output(self):
segids = []
rid = 0
segs = dict()
for i, block in enumerate(self.blocks):
# texts
texts, images, links, cssselectors = [], [], [], []
for node in block:
# extract text from node
for text in node.stripped_strings:
texts.append(text)
# extract text from node -- end
# extract images in css background
background_image_urls = self.__get_css_background_image_urls(node)
for url in background_image_urls:
dict_img = dict()
dict_img["alt"] = ""
dict_img["src"] = urljoin(self.url, url)
r, g, b = self.__get_css_background_color(node)
dict_img["bg_color"] = "%d,%d,%d" % (r, g, b)
images.append(dict_img)
# extract images in css background -- end
# extract images in <img> element
for img in node.find_all("img"):
dict_img = dict()
if "src" in img.attrs:
dict_img["src"] = urljoin(self.url, img["src"])
if "alt" in img.attrs:
dict_img["alt"] = img["alt"]
images.append(dict_img)
r, g, b = self.__get_css_background_color(img)
dict_img["bg_color"] = "%d,%d,%d" % (r, g, b)
# extract images in <img> element
# extract hyperlink from node
for link in node.find_all("a"):
if "href" in link.attrs:
links.append({"href": urljoin(self.url, link["href"])})
# extract hyperlink from node -- end
cssselectors.append(self.__get_css_selector(node))
if len(texts) == 0 and len(images) == 0:
continue
lid = block[0]["lid"]
if lid not in segids:
segids.append(lid)
sid = str(segids.index(lid))
if sid not in segs:
segs[sid] = {"segment_id": int(sid), "css_selector": self.__get_css_selector(block[0].parent), "records": []}
segs[sid]["records"].append(
{"record_id": rid, "texts": texts, "images": images, "css_selector": cssselectors, "links": links})
rid += 1
self.json_data = dict()
self.json_data["segments"] = [value for key, value in segs.items()]
self.json_data["url"] = self.url
self.json_data["title"] = self.browser.title
common.save_json(self.output_folder + "/result.json", self.json_data, encoding=setting.OUTPUT_JSON_ENCODING)
def __output_images(self):
tmp_path = self.output_folder + "/tmp"
path = self.output_folder + "/images"
common.prepare_clean_dir(tmp_path)
common.prepare_clean_dir(path)
for segment in self.json_data["segments"]:
for record in segment["records"]:
for i, image in enumerate(record["images"]):
try:
file_name = "%s_%s" % (record["record_id"], i)
source_file_name_only = tmp_path + "/" + file_name
original_extension = image["src"].split('/')[-1].split('.')[-1].split("?")[0]
source_file_name = source_file_name_only + "." + original_extension
target_file_name = path + "/" + file_name + "." + setting.OUTPUT_IMAGE_TYPE
r = requests.get(image["src"], stream=True, headers={'User-agent': 'Mozilla/5.0'})
if r.status_code == 200:
with open(source_file_name, 'wb') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
else:
continue
[R, G, B] = [int(a) for a in image["bg_color"].split(",")]
im = Image.open(source_file_name).convert('RGBA')
bg = Image.new("RGB", im.size, (R, G, B))
bg.paste(im, im)
im = bg
im.save(target_file_name)
image["path"] = target_file_name
except Exception:
pass
common.save_json(self.output_folder + "/result.json", self.json_data, encoding=setting.OUTPUT_JSON_ENCODING)
shutil.rmtree(tmp_path)
def remove_slash(self, path):
for i in range(len(path)):
if path.endswith('/'):
path = path[:-1]
if path.endswith('\\'):
path = path[:-1]
return path