diff --git a/scrapling/core/custom_types.py b/scrapling/core/custom_types.py index f952da7..0419406 100644 --- a/scrapling/core/custom_types.py +++ b/scrapling/core/custom_types.py @@ -89,6 +89,16 @@ class TextHandler(str): data = re.sub(' +', ' ', data) return self.__class__(data.strip()) + # For easy copy-paste from Scrapy/parsel code when needed :) + def get(self, default=None): + return self + + def get_all(self): + return self + + extract = get_all + extract_first = get + def json(self) -> Dict: """Return json response if the response is jsonable otherwise throw error""" # Using str function as a workaround for orjson issue with subclasses of str @@ -186,6 +196,19 @@ class TextHandlers(List[TextHandler]): return result return default + # For easy copy-paste from Scrapy/parsel code when needed :) + def get(self, default=None): + """Returns the first item of the current list + :param default: the default value to return if the current list is empty + """ + return self[0] if len(self) > 0 else default + + def extract(self): + return self + + extract_first = get + get_all = extract + class AttributesHandler(Mapping): """A read-only mapping to use instead of the standard dictionary for the speed boost but at the same time I use it to add more functionalities. diff --git a/scrapling/parser.py b/scrapling/parser.py index daaa8c4..169c9e7 100644 --- a/scrapling/parser.py +++ b/scrapling/parser.py @@ -330,6 +330,16 @@ class Adaptor(SelectorsGeneration): return self.__convert_results(prev_element) + # For easy copy-paste from Scrapy/parsel code when needed :) + def get(self, default=None): + return self + + def get_all(self): + return self + + extract = get_all + extract_first = get + def __str__(self) -> str: return self.html_content @@ -1073,12 +1083,19 @@ class Adaptors(List[Adaptor]): ] return self.__class__(results) if results else results + # For easy copy-paste from Scrapy/parsel code when needed :) def get(self, default=None): """Returns the first item of the current list :param default: the default value to return if the current list is empty """ return self[0] if len(self) > 0 else default + def extract(self): + return self + + extract_first = get + get_all = extract + @property def first(self): """Returns the first item of the current list or `None` if the list is empty"""