Saltar a contenido

Referencia de tipos personalizados

Aquí puedes consultar los parámetros, atributos y métodos de las clases de tipos personalizados de Scrapling.

Puedes importarlas directamente:

from scrapling.core.custom_types import TextHandler, TextHandlers, AttributesHandler

scrapling.core.custom_types.TextHandler

Clases base: str


              flowchart TD
              scrapling.core.custom_types.TextHandler[TextHandler]

              

              click scrapling.core.custom_types.TextHandler href "" "scrapling.core.custom_types.TextHandler"
            

Amplía las cadenas de Python con funciones adicionales.

__slots__ class-attribute instance-attribute

__slots__ = ()

extract class-attribute instance-attribute

extract = getall

extract_first class-attribute instance-attribute

extract_first = get

__getitem__

__getitem__(key)
Código fuente en scrapling/core/custom_types.py
def __getitem__(self, key: SupportsIndex | slice) -> "TextHandler":  # pragma: no cover
    lst = super().__getitem__(key)
    return TextHandler(lst)

split

split(sep=None, maxsplit=-1)
Código fuente en scrapling/core/custom_types.py
def split(self, sep: str | None = None, maxsplit: SupportsIndex = -1) -> list[Any]:  # pragma: no cover
    return TextHandlers([TextHandler(s) for s in super().split(sep, maxsplit)])

strip

strip(chars=None)
Código fuente en scrapling/core/custom_types.py
def strip(self, chars: str | None = None) -> Union[str, "TextHandler"]:  # pragma: no cover
    return TextHandler(super().strip(chars))

lstrip

lstrip(chars=None)
Código fuente en scrapling/core/custom_types.py
def lstrip(self, chars: str | None = None) -> Union[str, "TextHandler"]:  # pragma: no cover
    return TextHandler(super().lstrip(chars))

rstrip

rstrip(chars=None)
Código fuente en scrapling/core/custom_types.py
def rstrip(self, chars: str | None = None) -> Union[str, "TextHandler"]:  # pragma: no cover
    return TextHandler(super().rstrip(chars))

capitalize

capitalize()
Código fuente en scrapling/core/custom_types.py
def capitalize(self) -> Union[str, "TextHandler"]:  # pragma: no cover
    return TextHandler(super().capitalize())

casefold

casefold()
Código fuente en scrapling/core/custom_types.py
def casefold(self) -> Union[str, "TextHandler"]:  # pragma: no cover
    return TextHandler(super().casefold())

center

center(width, fillchar=' ')
Código fuente en scrapling/core/custom_types.py
def center(self, width: SupportsIndex, fillchar: str = " ") -> Union[str, "TextHandler"]:  # pragma: no cover
    return TextHandler(super().center(width, fillchar))

expandtabs

expandtabs(tabsize=8)
Código fuente en scrapling/core/custom_types.py
def expandtabs(self, tabsize: SupportsIndex = 8) -> Union[str, "TextHandler"]:  # pragma: no cover
    return TextHandler(super().expandtabs(tabsize))

format

format(*args, **kwargs)
Código fuente en scrapling/core/custom_types.py
def format(self, *args: object, **kwargs: object) -> Union[str, "TextHandler"]:  # pragma: no cover
    return TextHandler(super().format(*args, **kwargs))

format_map

format_map(mapping)
Código fuente en scrapling/core/custom_types.py
def format_map(self, mapping) -> Union[str, "TextHandler"]:  # pragma: no cover
    return TextHandler(super().format_map(mapping))

join

join(iterable)
Código fuente en scrapling/core/custom_types.py
def join(self, iterable: Iterable[str]) -> Union[str, "TextHandler"]:  # pragma: no cover
    return TextHandler(super().join(iterable))

ljust

ljust(width, fillchar=' ')
Código fuente en scrapling/core/custom_types.py
def ljust(self, width: SupportsIndex, fillchar: str = " ") -> Union[str, "TextHandler"]:  # pragma: no cover
    return TextHandler(super().ljust(width, fillchar))

rjust

rjust(width, fillchar=' ')
Código fuente en scrapling/core/custom_types.py
def rjust(self, width: SupportsIndex, fillchar: str = " ") -> Union[str, "TextHandler"]:  # pragma: no cover
    return TextHandler(super().rjust(width, fillchar))

swapcase

swapcase()
Código fuente en scrapling/core/custom_types.py
def swapcase(self) -> Union[str, "TextHandler"]:  # pragma: no cover
    return TextHandler(super().swapcase())

title

title()
Código fuente en scrapling/core/custom_types.py
def title(self) -> Union[str, "TextHandler"]:  # pragma: no cover
    return TextHandler(super().title())

translate

translate(table)
Código fuente en scrapling/core/custom_types.py
def translate(self, table) -> Union[str, "TextHandler"]:  # pragma: no cover
    return TextHandler(super().translate(table))

zfill

zfill(width)
Código fuente en scrapling/core/custom_types.py
def zfill(self, width: SupportsIndex) -> Union[str, "TextHandler"]:  # pragma: no cover
    return TextHandler(super().zfill(width))

replace

replace(old, new, count=-1)
Código fuente en scrapling/core/custom_types.py
def replace(self, old: str, new: str, count: SupportsIndex = -1) -> Union[str, "TextHandler"]:
    return TextHandler(super().replace(old, new, count))

upper

upper()
Código fuente en scrapling/core/custom_types.py
def upper(self) -> Union[str, "TextHandler"]:
    return TextHandler(super().upper())

lower

lower()
Código fuente en scrapling/core/custom_types.py
def lower(self) -> Union[str, "TextHandler"]:
    return TextHandler(super().lower())

sort

sort(reverse=False)

Devuelve una versión ordenada de la cadena.

Código fuente en scrapling/core/custom_types.py
def sort(self, reverse: bool = False) -> Union[str, "TextHandler"]:
    """Return a sorted version of the string"""
    return self.__class__("".join(sorted(self, reverse=reverse)))

clean

clean(remove_entities=False)

Devuelve una nueva versión de la cadena tras eliminar los espacios en blanco y los espacios consecutivos.

Código fuente en scrapling/core/custom_types.py
def clean(self, remove_entities=False) -> Union[str, "TextHandler"]:
    """Return a new version of the string after removing all white spaces and consecutive spaces"""
    data = self.translate(__CLEANING_TABLE__)
    if remove_entities:
        data = _replace_entities(data)
    return self.__class__(__CONSECUTIVE_SPACES_REGEX__.sub(" ", data).strip())

get

get(default=None)
Código fuente en scrapling/core/custom_types.py
def get(self, default=None):  # pragma: no cover
    return self

getall

getall()
Código fuente en scrapling/core/custom_types.py
def getall(self):  # pragma: no cover
    return self

json

json()

Devuelve la respuesta como JSON si se puede convertir; si no, lanza un error.

Código fuente en scrapling/core/custom_types.py
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.
    # Check this out: https://github.com/ijl/orjson/issues/445
    return loads(str(self))

re

re(
    regex,
    replace_entities=True,
    clean_match=False,
    case_sensitive=True,
    check_match=False,
)

Aplica la expresión regular al texto actual y devuelve una lista de cadenas con las coincidencias.

PARÁMETRO DESCRIPCIÓN
regex

Puede ser una expresión regular compilada o una cadena.

TIPO: str | Pattern

replace_entities

Si está activado, sustituye las referencias a entidades por sus caracteres correspondientes.

TIPO: bool PREDETERMINADO: True

clean_match

Si está activado, ignora los espacios en blanco y los espacios consecutivos al buscar coincidencias.

TIPO: bool PREDETERMINADO: False

case_sensitive

Si está desactivado, compila la expresión regular para que no distinga mayúsculas y minúsculas.

TIPO: bool PREDETERMINADO: True

check_match

Comprueba rápidamente si la expresión regular coincide, sin procesar los resultados.

TIPO: bool PREDETERMINADO: False

Código fuente en scrapling/core/custom_types.py
def re(
    self,
    regex: str | Pattern,
    replace_entities: bool = True,
    clean_match: bool = False,
    case_sensitive: bool = True,
    check_match: bool = False,
) -> Union["TextHandlers", bool]:
    """Apply the given regex to the current text and return a list of strings with the matches.

    :param regex: Can be either a compiled regular expression or a string.
    :param replace_entities: If enabled character entity references are replaced by their corresponding character
    :param clean_match: If enabled, this will ignore all whitespaces and consecutive spaces while matching
    :param case_sensitive: If disabled, function will set the regex to ignore the letters-case while compiling it
    :param check_match: Used to quickly check if this regex matches or not without any operations on the results

    """
    if isinstance(regex, str):
        if case_sensitive:
            regex = re_compile(regex, UNICODE)
        else:
            regex = re_compile(regex, flags=UNICODE | IGNORECASE)

    input_text = self.clean() if clean_match else self
    results = regex.findall(input_text)
    if check_match:
        return bool(results)

    if all(_is_iterable(res) for res in results):
        results = flatten(results)

    if not replace_entities:
        return TextHandlers([TextHandler(string) for string in results])

    return TextHandlers([TextHandler(_replace_entities(s)) for s in results])

re_first

re_first(
    regex,
    default=None,
    replace_entities=True,
    clean_match=False,
    case_sensitive=True,
)

Aplica la expresión regular al texto y devuelve la primera coincidencia, o el valor predeterminado si no encuentra ninguna.

PARÁMETRO DESCRIPCIÓN
regex

Puede ser una expresión regular compilada o una cadena.

TIPO: str | Pattern

default

Valor que se devuelve si no hay coincidencias.

TIPO: Any PREDETERMINADO: None

replace_entities

Si está activado, sustituye las referencias a entidades por sus caracteres correspondientes.

TIPO: bool PREDETERMINADO: True

clean_match

Si está activado, ignora los espacios en blanco y los espacios consecutivos al buscar coincidencias.

TIPO: bool PREDETERMINADO: False

case_sensitive

Si está desactivado, compila la expresión regular para que no distinga mayúsculas y minúsculas.

TIPO: bool PREDETERMINADO: True

Código fuente en scrapling/core/custom_types.py
def re_first(
    self,
    regex: str | Pattern,
    default: Any = None,
    replace_entities: bool = True,
    clean_match: bool = False,
    case_sensitive: bool = True,
) -> "TextHandler":
    """Apply the given regex to text and return the first match if found, otherwise return the default value.

    :param regex: Can be either a compiled regular expression or a string.
    :param default: The default value to be returned if there is no match
    :param replace_entities: If enabled character entity references are replaced by their corresponding character
    :param clean_match: If enabled, this will ignore all whitespaces and consecutive spaces while matching
    :param case_sensitive: If disabled, function will set the regex to ignore the letters-case while compiling it

    """
    result = self.re(
        regex,
        replace_entities,
        clean_match=clean_match,
        case_sensitive=case_sensitive,
    )
    return result[0] if result else default

scrapling.core.custom_types.TextHandlers

Clases base: List[TextHandler]


              flowchart TD
              scrapling.core.custom_types.TextHandlers[TextHandlers]

              

              click scrapling.core.custom_types.TextHandlers href "" "scrapling.core.custom_types.TextHandlers"
            

La clase :class:TextHandlers hereda de la clase integrada List y añade algunos métodos.

__slots__ class-attribute instance-attribute

__slots__ = ()

extract_first class-attribute instance-attribute

extract_first = get

getall class-attribute instance-attribute

getall = extract

__getitem__

__getitem__(pos)
Código fuente en scrapling/core/custom_types.py
def __getitem__(self, pos: SupportsIndex | slice) -> Union[TextHandler, "TextHandlers"]:
    lst = super().__getitem__(pos)
    if isinstance(pos, slice):
        return TextHandlers(cast(List[TextHandler], lst))
    return TextHandler(cast(TextHandler, lst))

re

re(
    regex,
    replace_entities=True,
    clean_match=False,
    case_sensitive=True,
)

Llama a .re() para cada elemento de esta lista y une los resultados en un TextHandlers, sin listas anidadas.

PARÁMETRO DESCRIPCIÓN
regex

Puede ser una expresión regular compilada o una cadena.

TIPO: str | Pattern

replace_entities

Si está activado, sustituye las referencias a entidades por sus caracteres correspondientes.

TIPO: bool PREDETERMINADO: True

clean_match

Si está activado, ignora los espacios en blanco y los espacios consecutivos al buscar coincidencias.

TIPO: bool PREDETERMINADO: False

case_sensitive

Si está desactivado, compila la expresión regular para que no distinga mayúsculas y minúsculas.

TIPO: bool PREDETERMINADO: True

Código fuente en scrapling/core/custom_types.py
def re(
    self,
    regex: str | Pattern,
    replace_entities: bool = True,
    clean_match: bool = False,
    case_sensitive: bool = True,
) -> "TextHandlers":
    """Call the ``.re()`` method for each element in this list and return
    their results flattened as TextHandlers.

    :param regex: Can be either a compiled regular expression or a string.
    :param replace_entities: If enabled character entity references are replaced by their corresponding character
    :param clean_match: if enabled, this will ignore all whitespaces and consecutive spaces while matching
    :param case_sensitive: if disabled, the function will set the regex to ignore the letters-case while compiling it
    """
    results = [n.re(regex, replace_entities, clean_match, case_sensitive) for n in self]
    return TextHandlers(flatten(results))

re_first

re_first(
    regex,
    default=None,
    replace_entities=True,
    clean_match=False,
    case_sensitive=True,
)

Llama a .re_first() para cada elemento de esta lista y devuelve el primer resultado, o el valor predeterminado si no hay ninguno.

PARÁMETRO DESCRIPCIÓN
regex

Puede ser una expresión regular compilada o una cadena.

TIPO: str | Pattern

default

Valor que se devuelve si no hay coincidencias.

TIPO: Any PREDETERMINADO: None

replace_entities

Si está activado, sustituye las referencias a entidades por sus caracteres correspondientes.

TIPO: bool PREDETERMINADO: True

clean_match

Si está activado, ignora los espacios en blanco y los espacios consecutivos al buscar coincidencias.

TIPO: bool PREDETERMINADO: False

case_sensitive

Si está desactivado, compila la expresión regular para que no distinga mayúsculas y minúsculas.

TIPO: bool PREDETERMINADO: True

Código fuente en scrapling/core/custom_types.py
def re_first(
    self,
    regex: str | Pattern,
    default: Any = None,
    replace_entities: bool = True,
    clean_match: bool = False,
    case_sensitive: bool = True,
) -> TextHandler:  # pragma: no cover
    """Call the ``.re_first()`` method for each element in this list and return
    the first result or the default value otherwise.

    :param regex: Can be either a compiled regular expression or a string.
    :param default: The default value to be returned if there is no match
    :param replace_entities: If enabled character entity references are replaced by their corresponding character
    :param clean_match: If enabled, this will ignore all whitespaces and consecutive spaces while matching
    :param case_sensitive: If disabled, function will set the regex to ignore the letters-case while compiling it
    """
    for n in self:
        for result in n.re(regex, replace_entities, clean_match, case_sensitive):
            return result
    return default

get

get(default=None)

Devuelve el primer elemento de la lista actual.

PARÁMETRO DESCRIPCIÓN
default

Valor que se devuelve si la lista actual está vacía.

PREDETERMINADO: None

Código fuente en scrapling/core/custom_types.py
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

extract

extract()
Código fuente en scrapling/core/custom_types.py
def extract(self):
    return self

scrapling.core.custom_types.AttributesHandler

AttributesHandler(mapping=None, **kwargs)

Clases base: Mapping[str, _TextHandlerType]


              flowchart TD
              scrapling.core.custom_types.AttributesHandler[AttributesHandler]

              

              click scrapling.core.custom_types.AttributesHandler href "" "scrapling.core.custom_types.AttributesHandler"
            

Mapping de solo lectura que sustituye al diccionario habitual para mejorar la velocidad y añadir funciones. Si necesitas un diccionario normal, conviértelo con la función dict.

Código fuente en scrapling/core/custom_types.py
def __init__(self, mapping: Any = None, **kwargs: Any) -> None:
    mapping = (
        {key: TextHandler(value) if isinstance(value, str) else value for key, value in mapping.items()}
        if mapping is not None
        else {}
    )

    if kwargs:
        mapping.update(
            {key: TextHandler(value) if isinstance(value, str) else value for key, value in kwargs.items()}
        )

    # Fastest read-only mapping type
    self._data: Mapping[str, Any] = MappingProxyType(mapping)

__slots__ class-attribute instance-attribute

__slots__ = ('_data',)

json_string property

json_string

Convierte los atributos actuales en bytes JSON si se pueden serializar. Si no, lanza un error.

get

get(key, default=None)

Funciona como el método .get() de un diccionario normal.

Código fuente en scrapling/core/custom_types.py
def get(self, key: str, default: Any = None) -> _TextHandlerType:
    """Acts like the standard dictionary `.get()` method"""
    return self._data.get(key, default)

search_values

search_values(keyword, partial=False)

Busca en los valores de los atributos actuales y devuelve un diccionario por cada coincidencia.

PARÁMETRO DESCRIPCIÓN
keyword

Texto que se busca en los valores de los atributos.

TIPO: str

partial

Si es True, comprueba si cada valor contiene el texto en lugar de exigir una coincidencia exacta.

TIPO: bool PREDETERMINADO: False

Código fuente en scrapling/core/custom_types.py
def search_values(self, keyword: str, partial: bool = False) -> Generator["AttributesHandler", None, None]:
    """Search current attributes by values and return a dictionary of each matching item
    :param keyword: The keyword to search for in the attribute values
    :param partial: If True, the function will search if keyword in each value instead of perfect match
    """
    for key, value in self._data.items():
        if partial:
            if keyword in value:
                yield AttributesHandler({key: value})
        else:
            if keyword == value:
                yield AttributesHandler({key: value})

__getitem__

__getitem__(key)
Código fuente en scrapling/core/custom_types.py
def __getitem__(self, key: str) -> _TextHandlerType:
    return self._data[key]

__iter__

__iter__()
Código fuente en scrapling/core/custom_types.py
def __iter__(self):
    return iter(self._data)

__len__

__len__()
Código fuente en scrapling/core/custom_types.py
def __len__(self):
    return len(self._data)

__repr__

__repr__()
Código fuente en scrapling/core/custom_types.py
def __repr__(self):
    return f"{self.__class__.__name__}({self._data})"

__str__

__str__()
Código fuente en scrapling/core/custom_types.py
def __str__(self):
    return str(self._data)

__contains__

__contains__(key)
Código fuente en scrapling/core/custom_types.py
def __contains__(self, key):
    return key in self._data