selectors.xpath.selectors
Selector for finding elements based on XPath, that allows any supported XPath expressions to be used with other soupsavvy components.
Classes
XPathSelector
- class XPathSelector(xpath: Any)[source]
Bases:
SoupSelectorSelector for finding elements based on XPath expressions.
Examples
>>> selector = XPathSelector("//p[@class='menu']") ... selector.find(soup)
Examples
>>> from lxml.etree import XPath ... selector = XPathSelector(XPath("//p[@class='menu']", smart_strings=False)) ... selector.find(soup)
Expressions must target elements, not attributes or text content.
Examples
>>> selector = XPathSelector("//div//@href") ... selector.find(soup) None
Notes
Equality check includes only xpath expression, as lxml XPath object does not implement more specific __eq__ method.
- __init__(xpath: Any) None[source]
Initializes XPathSelector with a given XPath expression.
Parameters
- xpathstr | lxml.etree.XPath
String representing of xpath expression or compiled XPath object. It needs to target elements, not attributes or text content.
Raises
- InvalidXPathSelector
If the provided XPath string cannot be compiled into XPath object.
- find_all(tag: IElement, recursive: bool = True, limit: int | None = None) list[IElement][source]
Finds all elements matching selector in provided IElement.
Parameters
- tagIElement
Any IElement object to search within.
- recursivebool, optional
Specifies if search should be recursive. If set to False, only direct children of the element will be searched. By default True.
- limitint, optional
Specifies maximum number of elements to return. By default None, all found elements are returned.
Returns
- list[IElement]
List of IElement objects matching selector. If none found, the list is empty.