Karim shoair commited on
Commit ·
32c7833
1
Parent(s): 21d6b5e
perf: Optimizing `next` and `previous` properties
Browse files- scrapling/parser.py +6 -8
scrapling/parser.py
CHANGED
|
@@ -426,10 +426,9 @@ class Selector(SelectorsGeneration):
|
|
| 426 |
def next(self) -> Optional["Selector"]:
|
| 427 |
"""Returns the next element of the current element in the children of the parent or ``None`` otherwise."""
|
| 428 |
next_element = self._root.getnext()
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
next_element = next_element.getnext()
|
| 433 |
|
| 434 |
return self.__handle_element(next_element)
|
| 435 |
|
|
@@ -437,10 +436,9 @@ class Selector(SelectorsGeneration):
|
|
| 437 |
def previous(self) -> Optional["Selector"]:
|
| 438 |
"""Returns the previous element of the current element in the children of the parent or ``None`` otherwise."""
|
| 439 |
prev_element = self._root.getprevious()
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
prev_element = prev_element.getprevious()
|
| 444 |
|
| 445 |
return self.__handle_element(prev_element)
|
| 446 |
|
|
|
|
| 426 |
def next(self) -> Optional["Selector"]:
|
| 427 |
"""Returns the next element of the current element in the children of the parent or ``None`` otherwise."""
|
| 428 |
next_element = self._root.getnext()
|
| 429 |
+
while next_element is not None and isinstance(next_element, html_forbidden):
|
| 430 |
+
# Ignore HTML comments and unwanted types
|
| 431 |
+
next_element = next_element.getnext()
|
|
|
|
| 432 |
|
| 433 |
return self.__handle_element(next_element)
|
| 434 |
|
|
|
|
| 436 |
def previous(self) -> Optional["Selector"]:
|
| 437 |
"""Returns the previous element of the current element in the children of the parent or ``None`` otherwise."""
|
| 438 |
prev_element = self._root.getprevious()
|
| 439 |
+
while prev_element is not None and isinstance(prev_element, html_forbidden):
|
| 440 |
+
# Ignore HTML comments and unwanted types
|
| 441 |
+
prev_element = prev_element.getprevious()
|
|
|
|
| 442 |
|
| 443 |
return self.__handle_element(prev_element)
|
| 444 |
|