MCPcopy Create free account
hub / github.com/EZ-hwh/AutoScraper / build_dom_tree

Function build_dom_tree

utils/step_test.py:48–76  ·  view source on GitHub ↗
(node, tot_node, father_index, node_list, attrs)

Source from the content-addressed store, hash-verified

46 return c
47
48def build_dom_tree(node, tot_node, father_index, node_list, attrs):
49 node_index = tot_node
50 new_node = Dom_TreeNode(tot_node, father_index)
51 tot_node = tot_node + 1
52 node_str = ''
53 if set(node.attrs.keys()) & set(attrs):
54 new_node.set_attr({x:y for x,y in node.attrs.items() if x in attrs})
55
56 for child in node.contents:
57 if isinstance(child, NavigableString):
58 if len(child.strip()) > 0:
59 text_node = Dom_TreeNode(tot_node, node_index)
60 tot_node = tot_node + 1
61 text_node.set_tag('text')
62 text_node.set_str(child.strip())
63 node_list.append(text_node)
64 new_node.append_children(tot_node - 1)
65
66 elif isinstance(child, Tag) and child.name not in ['script', 'style']:
67 child_index, tot_node = build_dom_tree(child, tot_node, node_index, node_list, attrs)
68 if child_index == -1:
69 continue
70 new_node.append_children(child_index)
71
72 new_node.set_tag(node.name)
73
74 new_node.set_str(node_str)
75 node_list.append(new_node)
76 return new_node.get_index(), tot_node
77
78def build_tree(html, attrs=[]):
79 tot_node = 0

Callers 1

build_treeFunction · 0.85

Calls 6

set_attrMethod · 0.95
set_tagMethod · 0.95
set_strMethod · 0.95
append_childrenMethod · 0.95
get_indexMethod · 0.95
Dom_TreeNodeClass · 0.85

Tested by

no test coverage detected