perf: Speeding up below_elements and relocate by 3%

This commit is contained in:
Karim shoair
2025-08-02 19:45:43 +03:00
parent d1a2ecd341
commit 67658f1723
+5 -2
View File
@@ -8,6 +8,7 @@ from cssselect import SelectorError, SelectorSyntaxError
from cssselect import parse as split_selectors from cssselect import parse as split_selectors
from lxml.html import HtmlElement, HtmlMixin, HTMLParser from lxml.html import HtmlElement, HtmlMixin, HTMLParser
from lxml.etree import ( from lxml.etree import (
XPath,
tostring, tostring,
fromstring, fromstring,
XPathError, XPathError,
@@ -47,6 +48,8 @@ _whitelisted = {
"class_": "class", "class_": "class",
"for_": "for", "for_": "for",
} }
# Pre-compiled selectors for efficiency
_find_all_elements = XPath(".//*")
class Selector(SelectorsGeneration): class Selector(SelectorsGeneration):
@@ -380,7 +383,7 @@ class Selector(SelectorsGeneration):
@property @property
def below_elements(self) -> "Selectors": def below_elements(self) -> "Selectors":
"""Return all elements under the current element in the DOM tree""" """Return all elements under the current element in the DOM tree"""
below = self._root.xpath(".//*") below = _find_all_elements(self._root)
return self.__handle_elements(below) return self.__handle_elements(below)
@property @property
@@ -496,7 +499,7 @@ class Selector(SelectorsGeneration):
if issubclass(type(element), HtmlElement): if issubclass(type(element), HtmlElement):
element = _StorageTools.element_to_dict(element) element = _StorageTools.element_to_dict(element)
for node in self._root.iter("*"): for node in _find_all_elements(self._root):
# Collect all elements in the page, then for each element get the matching score of it against the node. # Collect all elements in the page, then for each element get the matching score of it against the node.
# Hence: the code doesn't stop even if the score was 100% # Hence: the code doesn't stop even if the score was 100%
# because there might be another element(s) left in page with the same score # because there might be another element(s) left in page with the same score