Skip to main content
The TextHandler class extends Python’s standard str class to provide enhanced text processing capabilities including regex operations, JSON parsing, cleaning, and more.

Overview

TextHandler is used throughout Scrapling to represent text content. It’s returned by properties like Selector.text and methods like get(), re(), etc. It maintains all standard string functionality while adding powerful text manipulation methods.

String Methods

All standard Python string methods are available and return TextHandler objects:
  • strip(), lstrip(), rstrip() - Remove whitespace
  • upper(), lower(), capitalize(), title(), swapcase(), casefold() - Case conversion
  • replace() - Replace substrings
  • split() - Split into list (returns list of TextHandler objects)
  • join() - Join iterable
  • center(), ljust(), rjust(), zfill() - Alignment and padding
  • expandtabs(), translate() - Text transformation
  • format(), format_map() - String formatting
And many more standard string methods.

Enhanced Methods

clean()

Return a new version of the string after removing all white spaces and consecutive spaces.
bool
default:"false"
If True, also replaces HTML entities with their corresponding characters
Returns: A cleaned TextHandler object Example:

sort()

Return a sorted version of the string.
bool
default:"false"
If True, sort in descending order
Returns: A TextHandler with characters sorted Example:

json()

Return JSON response if the string is valid JSON. Returns: A dictionary parsed from the JSON string Raises: Exception if the string is not valid JSON Example:

re()

Apply the given regex to the current text and return a list of strings with the matches.
str | Pattern
required
Can be either a compiled regular expression or a string
bool
default:"true"
If enabled, character entity references are replaced by their corresponding character in results
bool
default:"false"
If enabled, ignores all whitespaces and consecutive spaces while matching
bool
default:"true"
If disabled, the regex will ignore letter case while matching
bool
default:"false"
Used to quickly check if this regex matches or not without any operations on the results. Returns boolean instead of matches
Returns: A TextHandlers list of matches, or a boolean if check_match=True Example:

re_first()

Apply the given regex to text and return the first match if found, otherwise return the default value.
str | Pattern
required
Can be either a compiled regular expression or a string
Any
default:"None"
The default value to be returned if there is no match
bool
default:"true"
If enabled, character entity references are replaced by their corresponding character
bool
default:"false"
If enabled, ignores all whitespaces and consecutive spaces while matching
bool
default:"true"
If disabled, the regex will ignore letter case while matching
Returns: A TextHandler with the first match or the default value Example:

Compatibility Methods

For compatibility with Scrapy/Parsel:

get()

Returns self (for Scrapy/Parsel compatibility). Example:

get_all()

Returns self (for Scrapy/Parsel compatibility). Example:

Aliases

  • extract() - alias for get_all()
  • extract_first() - alias for get()

Common Use Cases

Extract Numbers

Clean and Normalize Text

Parse Structured Data

Extract Multiple Values

Handle HTML Entities

Indexing and Slicing

String Operations

Notes

  • TextHandler is a subclass of str, so all standard string operations work
  • Methods that modify strings return new TextHandler objects (strings are immutable)
  • The clean() method is particularly useful for normalizing scraped text
  • Regex methods use the re module internally and support all Python regex features
  • JSON parsing uses orjson for high performance
  • HTML entity replacement uses the w3lib library

See Also

  • Selectors - List container that uses TextHandler for text operations
  • Selector - Uses TextHandler for text content via the text property
  • AttributesHandler - Uses TextHandler for attribute values