From 57090f90a4856a5be92553c824f4ebb1276e360a Mon Sep 17 00:00:00 2001 From: Karim shoair Date: Thu, 30 Jan 2025 23:39:46 +0200 Subject: [PATCH] refactor(parser/find_all): about ~95% speed increase Boom! --- scrapling/parser.py | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/scrapling/parser.py b/scrapling/parser.py index cadfa82..4e96501 100644 --- a/scrapling/parser.py +++ b/scrapling/parser.py @@ -593,14 +593,6 @@ class Adaptor(SelectorsGeneration): tags, patterns = set(), set() results, functions, selectors = Adaptors([]), [], [] - def _search_tree(element: Adaptor, filter_function: Callable) -> None: - """Collect element if it fulfills passed function otherwise, traverse the children tree and iterate""" - if filter_function(element): - results.append(element) - - for branch in element.children: - _search_tree(branch, filter_function) - # Brace yourself for a wonderful journey! for arg in args: if type(arg) is str: @@ -661,9 +653,9 @@ class Adaptor(SelectorsGeneration): for pattern in patterns: results.extend(self.find_by_regex(pattern, first_match=False)) - for result in (results or [self]): - for function in functions: - _search_tree(result, function) + # Collect element if it fulfills passed function otherwise + for function in functions: + results.extend((results or self.below_elements).filter(function)) return results