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

Function build_absolute_xpath

utils/html_utils.py:60–73  ·  view source on GitHub ↗

Constructs an absolute XPath expression for a given node. :param node: A lxml node for which to build the XPath :return: A string representing the absolute XPath to the node

(node)

Source from the content-addressed store, hash-verified

58 return None
59
60def build_absolute_xpath(node):
61 """
62 Constructs an absolute XPath expression for a given node.
63 :param node: A lxml node for which to build the XPath
64 :return: A string representing the absolute XPath to the node
65 """
66 parts = []
67 while node is not None and isinstance(node, _Element) and node.tag != 'html':
68 parent = node.getparent()
69 siblings = [sib for sib in parent.iterchildren() if sib.tag == node.tag]
70 count = siblings.index(node) + 1
71 parts.insert(0, f"{node.tag}[{count}]")
72 node = parent
73 return '/html/' + '/'.join(parts)
74
75def simplify_html(html, reserve_attrs = ['class']):
76 soup = BeautifulSoup(html, 'html.parser')

Callers 1

get_absolute_xpathFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected