Skip to content

🕷️ The BEST Web Scraping framework that handles everything from a single request to a full-scale crawl! Don't be shy, join here: https://discord.gg/EMgGbDceNQ and follow here for daily tips and tricks: https://x.com/Scrapling_dev

perf: Optimizing `next` and `previous` properties

Karim shoairaec4889d25f3
diff --git a/scrapling/parser.py b/scrapling/parser.py
index e6a6af3..8146461 100644
--- a/scrapling/parser.py
+++ b/scrapling/parser.py
@@ -426,10 +426,9 @@ class Selector(SelectorsGeneration):
def next(self) -> Optional["Selector"]:
"""Returns the next element of the current element in the children of the parent or ``None`` otherwise."""
next_element = self._root.getnext()
- if next_element is not None:
- while isinstance(next_element, html_forbidden):
- # Ignore HTML comments and unwanted types
- next_element = next_element.getnext()
+ while next_element is not None and isinstance(next_element, html_forbidden):
+ # Ignore HTML comments and unwanted types
+ next_element = next_element.getnext()
return self.__handle_element(next_element)
@@ -437,10 +436,9 @@ class Selector(SelectorsGeneration):
def previous(self) -> Optional["Selector"]:
"""Returns the previous element of the current element in the children of the parent or ``None`` otherwise."""
prev_element = self._root.getprevious()
- if prev_element is not None:
- while isinstance(prev_element, html_forbidden):
- # Ignore HTML comments and unwanted types
- prev_element = prev_element.getprevious()
+ while prev_element is not None and isinstance(prev_element, html_forbidden):
+ # Ignore HTML comments and unwanted types
+ prev_element = prev_element.getprevious()
return self.__handle_element(prev_element)