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 returnTextHandler objects:
strip(),lstrip(),rstrip()- Remove whitespaceupper(),lower(),capitalize(),title(),swapcase(),casefold()- Case conversionreplace()- Replace substringssplit()- Split into list (returns list ofTextHandlerobjects)join()- Join iterablecenter(),ljust(),rjust(),zfill()- Alignment and paddingexpandtabs(),translate()- Text transformationformat(),format_map()- String formatting
Enhanced Methods
clean()
bool
default:"false"
If True, also replaces HTML entities with their corresponding characters
TextHandler object
Example:
sort()
bool
default:"false"
If True, sort in descending order
TextHandler with characters sorted
Example:
json()
re()
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
TextHandlers list of matches, or a boolean if check_match=True
Example:
re_first()
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
TextHandler with the first match or the default value
Example:
Compatibility Methods
For compatibility with Scrapy/Parsel:get()
get_all()
Aliases
extract()- alias forget_all()extract_first()- alias forget()
Common Use Cases
Extract Numbers
Clean and Normalize Text
Parse Structured Data
Extract Multiple Values
Case-Insensitive Search
Handle HTML Entities
Indexing and Slicing
String Operations
Notes
TextHandleris a subclass ofstr, so all standard string operations work- Methods that modify strings return new
TextHandlerobjects (strings are immutable) - The
clean()method is particularly useful for normalizing scraped text - Regex methods use the
remodule internally and support all Python regex features - JSON parsing uses
orjsonfor high performance - HTML entity replacement uses the
w3liblibrary
See Also
- Selectors - List container that uses
TextHandlerfor text operations - Selector - Uses
TextHandlerfor text content via thetextproperty - AttributesHandler - Uses
TextHandlerfor attribute values