Skip to main content
Scrapling provides custom string and attribute handling types that extend Python’s built-in types with additional functionality for web scraping workflows.

TextHandler

Extended string type with scraping-specific methods.

Class Definition

Additional Methods

clean

Return a cleaned version of the string after removing whitespace and consecutive spaces.
bool
default:"False"
If True, also replaces HTML entities with their characters.
Example:

re

Apply regex to the text and return matches.
str | Pattern
required
Regular expression pattern (string or compiled).
bool
default:"True"
Replace HTML entities in matches.
bool
default:"False"
Clean whitespace before matching.
bool
default:"True"
Whether to match case-sensitively.
bool
default:"False"
If True, return bool indicating if pattern matches (no results extraction).
Returns: TextHandlers list of matches, or bool if check_match=True Example:

re_first

Apply regex and return the first match, or default if no match.
str | Pattern
required
Regular expression pattern.
Any
default:"None"
Value to return if no match is found.
bool
default:"True"
Replace HTML entities in the match.
bool
default:"False"
Clean whitespace before matching.
bool
default:"True"
Whether to match case-sensitively.
Example:

json

Parse the text as JSON. Returns: Parsed JSON as dictionary Raises: JSON parsing error if invalid Example:

sort

Return a sorted version of the string.
bool
default:"False"
Sort in descending order.
Example:

Scrapy/Parsel Compatibility

For easy migration from Scrapy/Parsel:

TextHandlers

List of TextHandler objects with collective operations.

Class Definition

Methods

re

Apply regex to each element and return flattened results. Example:

re_first

Apply regex to each element and return the first match overall. Example:

get

Return the first element, or default if empty. Example:

Scrapy/Parsel Compatibility

AttributesHandler

Read-only mapping for HTML element attributes with additional functionality.

Class Definition

Constructor

Example:

Methods

get

Get an attribute value by key. Example:

search_values

Search attributes by value.
str
required
Keyword to search for in values.
bool
default:"False"
If True, use substring matching. If False, require exact match.
Yields: AttributesHandler for each matching key-value pair Example:

json_string

Convert attributes to JSON bytes. Example:

Usage with Responses

Complete Examples

Text Cleaning and Extraction

Working with Lists

Attribute Handling

Spider Integration

Type Hints

For type checking in your code:

See Also

  • Response - Response objects return these types
  • Spider - Using custom types in spider callbacks