Selectors class is a subclass of Python’s builtin List class that provides additional methods for working with collections of Selector objects. It maintains all standard list functionality while adding powerful batch processing capabilities.
Overview
Selectors is returned by most selection methods like css(), xpath(), find_all(), and others when multiple elements are found. It allows you to perform operations on all contained selectors at once.
Properties
first
Selector or None if the list is empty
Example:
last
Selector or None if the list is empty
Example:
length
Selection Methods
xpath()
.xpath() method for each element in this list and return their results as another Selectors object.
The XPath selector to be used
A string that will be used to retrieve element’s data in adaptive, otherwise the selector will be used. Recommended if you plan to use a different selector later and want to relocate the same element(s)
Automatically save new elements for adaptive later
The minimum percentage to accept while adaptive is working. Don’t play with this number unless you know what you’re doing
Additional keyword arguments will be passed as XPath variables in the XPath expression
Selectors object with flattened results
Example:
css()
.css() method for each element in this list and return their results flattened as another Selectors object.
The CSS3 selector to be used
A string that will be used to retrieve element’s data in adaptive, otherwise the selector will be used. Recommended if you plan to use a different selector later and want to relocate the same element(s)
Automatically save new elements for adaptive later
The minimum percentage to accept while adaptive is working. Don’t play with this number unless you know what you’re doing
Selectors object with flattened results
Example:
Extraction Methods
get()
default if empty.
The default value to return if the current list is empty
TextHandler containing the first element’s content, or the default value
Example:
getall()
TextHandlers list containing all elements’ content
Example:
re()
.re() method for each element in this list and return their results flattened as a list of TextHandler.
Can be either a compiled regular expression or a string
If enabled, character entity references are replaced by their corresponding character
If enabled, this will ignore all whitespaces and consecutive spaces while matching
If disabled, the function will set the regex to ignore the letters case while compiling it
TextHandlers list with all matches from all elements
Example:
re_first()
.re_first() method for each element in this list and return the first result or the default value otherwise.
Can be either a compiled regular expression or a string
The default value to be returned if there is no match
If enabled, character entity references are replaced by their corresponding character
If enabled, this will ignore all whitespaces and consecutive spaces while matching
If disabled, the function will set the regex to ignore the letters case while compiling it
TextHandler with the first match or the default value
Example:
Filter Methods
search()
A function that takes each element as an argument and returns True/False
Selector that matches the function or None otherwise
Example:
filter()
A function that takes each element as an argument and returns True/False
Selectors object containing only matching elements, or empty list
Example:
Magic Methods
__getitem__()
Selector when using an index, or a new Selectors object when using a slice
Example:
List Operations
SinceSelectors is a subclass of list, all standard list methods are available:
append(item)- Add a selector to the endextend(items)- Add multiple selectorsinsert(index, item)- Insert at a specific positionremove(item)- Remove first occurrencepop(index)- Remove and return item at indexclear()- Remove all itemsindex(item)- Find index of itemcount(item)- Count occurrencesreverse()- Reverse in placesort()- Sort in place
Aliases
For compatibility with Scrapy/Parsel:extract()- alias forgetall()extract_first()- alias forget()
Notes
- Results from
css()andxpath()methods are automatically flattened, so nested selections work seamlessly - The class cannot be pickled due to underlying lxml limitations
- Empty
Selectorsobjects evaluate toFalsein boolean contexts