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

Better logic for `css_first` and `xpath_first`

Karim shoair6b40af426fc9
diff --git a/scrapling/parser.py b/scrapling/parser.py
index 583b1c5..fdf3bda 100644
--- a/scrapling/parser.py
+++ b/scrapling/parser.py
@@ -416,10 +416,9 @@ class Adaptor(SelectorsGeneration):
:return: List as :class:`Adaptors`
"""
- try:
- return self.css(selector, identifier, auto_match, auto_save, percentage)[0]
- except (IndexError, TypeError,):
- return None
+ for element in self.css(selector, identifier, auto_match, auto_save, percentage):
+ return element
+ return None
def xpath_first(self, selector: str, identifier: str = '',
auto_match: bool = False, auto_save: bool = False, percentage: int = 0, **kwargs: Any
@@ -443,10 +442,9 @@ class Adaptor(SelectorsGeneration):
:return: List as :class:`Adaptors`
"""
- try:
- return self.xpath(selector, identifier, auto_match, auto_save, percentage, **kwargs)[0]
- except (IndexError, TypeError,):
- return None
+ for element in self.xpath(selector, identifier, auto_match, auto_save, percentage, **kwargs):
+ return element
+ return None
def css(self, selector: str, identifier: str = '',
auto_match: bool = False, auto_save: bool = False, percentage: int = 0