Selector class is the core component of Scrapling that wraps HTML content and provides powerful methods for selecting and extracting data using CSS, XPath, or text-based queries.
Constructor
str | bytes
required
HTML content as either string or bytes
str
default:""
Store a URL with the HTML data for retrieving later
str
default:"utf-8"
The encoding type used in HTML parsing
bool
default:"true"
Should always be enabled when parsing large HTML documents. Controls the libxml2 feature that forbids parsing certain large documents to protect from possible memory exhaustion
HtmlElement
default:"None"
Used internally to pass etree objects. Don’t use unless you know what you’re doing
bool
default:"false"
Whether to drop comments while parsing the HTML body
bool
default:"false"
Whether to drop CDATA while parsing the HTML body
bool
default:"false"
Globally turn off the adaptive feature in all functions. Takes higher priority over all adaptive related arguments/functions in the class
Any
default:"SQLiteStorageSystem"
The storage class to be passed for adaptive functionalities
Dict
default:"None"
A dictionary of argument-value pairs to be passed for the storage class. If empty, default values will be used
Properties
tag
"#text" for text nodes
text
TextHandler object containing the element’s text
attrib
AttributesHandler object containing the element’s attributes
html_content
TextHandler containing the element’s inner HTML
body
parent
Selector or None if there is no parent
children
Selectors object containing child elements, or empty list if none
siblings
Selectors object containing sibling elements, or empty list if none
below_elements
Selectors object containing all descendant elements
path
Selectors that contains the path leading to the current element from the root.
Returns: A Selectors object representing the element’s path
next
Selector or None if there isn’t one
previous
Selector or None if there isn’t one
Selection Methods
css()
str
required
The CSS3 selector to be used
str
default:""
A string that will be used to save/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)
bool
default:"false"
Enabled will make the function try to relocate the element if it was saved before
bool
default:"false"
Automatically save new elements for adaptive later
int
default:"0"
The minimum percentage to accept while adaptive is working. Don’t play with this number unless you know what you’re doing
Selectors object
xpath()
str
required
The XPath selector to be used
str
default:""
A string that will be used to save/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)
bool
default:"false"
Enabled will make the function try to relocate the element if it was saved before
bool
default:"false"
Automatically save new elements for adaptive later
int
default:"0"
The minimum percentage to accept while adaptive is working. Don’t play with this number unless you know what you’re doing
Any
Additional keyword arguments will be passed as XPath variables in the XPath expression
Selectors object
find_all()
str | Iterable[str] | Pattern | Callable | Dict[str, str]
Tag name(s), iterable of tag names, regex patterns, function, or a dictionary of elements’ attributes. Leave empty for selecting all
str
The attributes you want to filter elements based on
Selectors object of the elements or empty list
find()
str | Iterable[str] | Pattern | Callable | Dict[str, str]
Tag name(s), iterable of tag names, regex patterns, function, or a dictionary of elements’ attributes. Leave empty for selecting all
str
The attributes you want to filter elements based on
Selector object or None if the result didn’t match
find_by_text()
str
required
Text query to match
bool
default:"true"
Returns the first element that matches conditions
bool
default:"false"
If enabled, the function returns elements that contain the input text
bool
default:"false"
If enabled, the letters case will be taken into consideration
bool
default:"true"
If enabled, this will ignore all whitespaces and consecutive spaces while matching
Selector if first_match=True, otherwise Selectors
find_by_regex()
str | Pattern[str]
required
Regex query/pattern to match
bool
default:"true"
Return the first element that matches conditions
bool
default:"false"
If enabled, the letters case will be taken into consideration in the regex
bool
default:"true"
If enabled, this will ignore all whitespaces and consecutive spaces while matching
Selector if first_match=True, otherwise Selectors
find_similar()
float
default:"0.2"
The percentage to use while comparing element attributes. Don’t play with this number unless you’re getting unwanted results
List | Tuple
default:"('href', 'src')"
Attribute names to ignore while matching attributes. Default ignores
href and src as URLs can change between elementsbool
default:"false"
If True, element text content will be taken into calculation while matching. Not recommended in normal cases
Selectors container of Selector objects or empty list
Extraction Methods
get_all_text()
str
default:"\\n"
Strings will be concatenated using this separator
bool
default:"false"
If True, strings will be stripped before being concatenated
Tuple
default:"('script', 'style')"
A tuple of all tag names you want to ignore
bool
default:"true"
If enabled, elements with text-content that is empty or only whitespaces will be ignored
TextHandler object
get()
TextHandler containing the serialized string
getall()
TextHandlers list with one element
json()
re()
str | Pattern[str]
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
bool
default:"false"
If enabled, this will ignore all whitespaces and consecutive spaces while matching
bool
default:"true"
If disabled, the function will set the regex to ignore the letters case while compiling it
TextHandlers list of matches
re_first()
str | Pattern[str]
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, this will ignore all whitespaces and consecutive spaces while matching
bool
default:"true"
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
Utility Methods
prettify()
TextHandler with formatted HTML
has_class()
str
required
The class name to check for
True if element has class with that name, otherwise False
urljoin()
str
required
The relative URL to join
iterancestors()
Selector objects
find_ancestor()
Callable[[Selector], bool]
required
A function that takes each ancestor as an argument and returns True/False
None otherwise
Adaptive Methods
save()
HtmlElement
required
The element itself to save to storage. Can be a
Selector or pure HtmlElementstr
required
The identifier that will be used to retrieve the element later from the storage
retrieve()
str
required
The identifier used to retrieve the element from the storage
None
relocate()
Union[Dict, HtmlElement, Selector]
required
The element to relocate in the tree
int
default:"0"
The minimum percentage to accept. Don’t play with this number unless you know what you’re doing
bool
default:"false"
If True, the return result will be converted to
Selectors objectSelectors object
Magic Methods
__getitem__()
__contains__()
Aliases
For compatibility with Scrapy/Parsel:extract()- alias forgetall()extract_first()- alias forget()