Adding empty methods (get/get_all/extract/extract_all)
For easy copy-paste from Scrapy/parsel code when needed :)
This commit is contained in:
@@ -89,6 +89,16 @@ class TextHandler(str):
|
|||||||
data = re.sub(' +', ' ', data)
|
data = re.sub(' +', ' ', data)
|
||||||
return self.__class__(data.strip())
|
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:
|
def json(self) -> Dict:
|
||||||
"""Return json response if the response is jsonable otherwise throw error"""
|
"""Return json response if the response is jsonable otherwise throw error"""
|
||||||
# Using str function as a workaround for orjson issue with subclasses of str
|
# Using str function as a workaround for orjson issue with subclasses of str
|
||||||
@@ -186,6 +196,19 @@ class TextHandlers(List[TextHandler]):
|
|||||||
return result
|
return result
|
||||||
return default
|
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):
|
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.
|
"""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.
|
||||||
|
|||||||
@@ -330,6 +330,16 @@ class Adaptor(SelectorsGeneration):
|
|||||||
|
|
||||||
return self.__convert_results(prev_element)
|
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:
|
def __str__(self) -> str:
|
||||||
return self.html_content
|
return self.html_content
|
||||||
|
|
||||||
@@ -1073,12 +1083,19 @@ class Adaptors(List[Adaptor]):
|
|||||||
]
|
]
|
||||||
return self.__class__(results) if results else results
|
return self.__class__(results) if results else results
|
||||||
|
|
||||||
|
# For easy copy-paste from Scrapy/parsel code when needed :)
|
||||||
def get(self, default=None):
|
def get(self, default=None):
|
||||||
"""Returns the first item of the current list
|
"""Returns the first item of the current list
|
||||||
:param default: the default value to return if the current list is empty
|
:param default: the default value to return if the current list is empty
|
||||||
"""
|
"""
|
||||||
return self[0] if len(self) > 0 else default
|
return self[0] if len(self) > 0 else default
|
||||||
|
|
||||||
|
def extract(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
extract_first = get
|
||||||
|
get_all = extract
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def first(self):
|
def first(self):
|
||||||
"""Returns the first item of the current list or `None` if the list is empty"""
|
"""Returns the first item of the current list or `None` if the list is empty"""
|
||||||
|
|||||||
Reference in New Issue
Block a user