跳转至

自定义类型参考

这里列出了 Scrapling 自定义类型的参数、属性和方法。

可以直接导入:

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

scrapling.core.custom_types.TextHandler

基类: str


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

              

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

扩展标准 Python 字符串,提供更多功能。

__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)
源代码位于 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)
源代码位于 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)
源代码位于 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)
源代码位于 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)
源代码位于 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()
源代码位于 scrapling/core/custom_types.py
def capitalize(self) -> Union[str, "TextHandler"]:  # pragma: no cover
    return TextHandler(super().capitalize())

casefold

casefold()
源代码位于 scrapling/core/custom_types.py
def casefold(self) -> Union[str, "TextHandler"]:  # pragma: no cover
    return TextHandler(super().casefold())

center

center(width, fillchar=' ')
源代码位于 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)
源代码位于 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)
源代码位于 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)
源代码位于 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)
源代码位于 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=' ')
源代码位于 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=' ')
源代码位于 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()
源代码位于 scrapling/core/custom_types.py
def swapcase(self) -> Union[str, "TextHandler"]:  # pragma: no cover
    return TextHandler(super().swapcase())

title

title()
源代码位于 scrapling/core/custom_types.py
def title(self) -> Union[str, "TextHandler"]:  # pragma: no cover
    return TextHandler(super().title())

translate

translate(table)
源代码位于 scrapling/core/custom_types.py
def translate(self, table) -> Union[str, "TextHandler"]:  # pragma: no cover
    return TextHandler(super().translate(table))

zfill

zfill(width)
源代码位于 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)
源代码位于 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()
源代码位于 scrapling/core/custom_types.py
def upper(self) -> Union[str, "TextHandler"]:
    return TextHandler(super().upper())

lower

lower()
源代码位于 scrapling/core/custom_types.py
def lower(self) -> Union[str, "TextHandler"]:
    return TextHandler(super().lower())

sort

sort(reverse=False)

返回排序后的字符串。

源代码位于 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)

移除所有空白字符和连续空格,返回新的字符串。

源代码位于 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)
源代码位于 scrapling/core/custom_types.py
def get(self, default=None):  # pragma: no cover
    return self

getall

getall()
源代码位于 scrapling/core/custom_types.py
def getall(self):  # pragma: no cover
    return self

json

json()

将响应解析为 JSON,无法解析时抛出错误。

源代码位于 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,
)

对当前文本应用给定的正则表达式,并返回包含匹配字符串的列表。

参数 说明
regex

可以是编译后的正则表达式,也可以是字符串。

类型: str | Pattern

replace_entities

启用后,将字符实体引用替换为对应字符。

类型: bool 默认值: True

clean_match

启用后,匹配时忽略所有空白字符和连续空格。

类型: bool 默认值: False

case_sensitive

关闭后,编译正则表达式时忽略大小写。

类型: bool 默认值: True

check_match

快速检查正则表达式是否匹配,不对结果进行其他处理。

类型: bool 默认值: False

源代码位于 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,
)

对文本应用给定的正则表达式,并返回第一个匹配结果。如果没有,则返回默认值。

参数 说明
regex

可以是编译后的正则表达式,也可以是字符串。

类型: str | Pattern

default

没有匹配结果时返回的默认值。

类型: Any 默认值: None

replace_entities

启用后,将字符实体引用替换为对应字符。

类型: bool 默认值: True

clean_match

启用后,匹配时忽略所有空白字符和连续空格。

类型: bool 默认值: False

case_sensitive

关闭后,编译正则表达式时忽略大小写。

类型: bool 默认值: True

源代码位于 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

基类: List[TextHandler]


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

              

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

:class:TextHandlers 继承内置的 List 类,并提供一些额外方法。

__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)
源代码位于 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,
)

对列表中每个元素调用 .re(),将结果展平后作为 TextHandlers 返回,不包含嵌套列表。

参数 说明
regex

可以是编译后的正则表达式,也可以是字符串。

类型: str | Pattern

replace_entities

启用后,将字符实体引用替换为对应字符。

类型: bool 默认值: True

clean_match

启用后,匹配时会忽略所有空白及连续空格。

类型: bool 默认值: False

case_sensitive

关闭后,编译正则表达式时忽略大小写。

类型: bool 默认值: True

源代码位于 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,
)

对列表中每个元素调用 .re_first(),返回第一个匹配结果,没有匹配时返回默认值。

参数 说明
regex

可以是编译后的正则表达式,也可以是字符串。

类型: str | Pattern

default

没有匹配结果时返回的默认值。

类型: Any 默认值: None

replace_entities

启用后,将字符实体引用替换为对应字符。

类型: bool 默认值: True

clean_match

启用后,匹配时忽略所有空白字符和连续空格。

类型: bool 默认值: False

case_sensitive

关闭后,编译正则表达式时忽略大小写。

类型: bool 默认值: True

源代码位于 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)

返回当前列表的第一个元素。

参数 说明
default

当前列表为空时返回的默认值。

默认值: None

源代码位于 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()
源代码位于 scrapling/core/custom_types.py
def extract(self):
    return self

scrapling.core.custom_types.AttributesHandler

AttributesHandler(mapping=None, **kwargs)

基类: Mapping[str, _TextHandlerType]


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

              

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

用于替代标准字典的只读映射,速度更快,并提供额外功能。如需普通字典,可以使用 dict 函数转换。

源代码位于 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

将当前属性转换为 JSON 字节,如果属性无法序列化为 JSON,则抛出错误。

get

get(key, default=None)

与标准字典的 .get() 方法相同。

源代码位于 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)

按值搜索当前属性,并为每个匹配项返回一个字典。

参数 说明
keyword

要在属性值中查找的文本。

类型: str

partial

为 True 时,检查每个值是否包含指定文本,而不是要求完全匹配。

类型: bool 默认值: False

源代码位于 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)
源代码位于 scrapling/core/custom_types.py
def __getitem__(self, key: str) -> _TextHandlerType:
    return self._data[key]

__iter__

__iter__()
源代码位于 scrapling/core/custom_types.py
def __iter__(self):
    return iter(self._data)

__len__

__len__()
源代码位于 scrapling/core/custom_types.py
def __len__(self):
    return len(self._data)

__repr__

__repr__()
源代码位于 scrapling/core/custom_types.py
def __repr__(self):
    return f"{self.__class__.__name__}({self._data})"

__str__

__str__()
源代码位于 scrapling/core/custom_types.py
def __str__(self):
    return str(self._data)

__contains__

__contains__(key)
源代码位于 scrapling/core/custom_types.py
def __contains__(self, key):
    return key in self._data