Adding empty methods (get/get_all/extract/extract_all)

For easy copy-paste from Scrapy/parsel code when needed :)
This commit is contained in:
Karim shoair
2024-12-04 14:28:37 +02:00
parent 31e838ce9b
commit 45e86f50a3
2 changed files with 40 additions and 0 deletions
+23
View File
@@ -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.