Given an XPath selector, returns a list of :class:`Element ` objects or a single one. :param selector: XPath Selector to use. :param clean: Whether or not to sanitize the found HTML of `` `` and `` `` tags. :param first: Whether or not to return
(self, selector: str, *, clean: bool = False, first: bool = False, _encoding: str = None)
| 234 | return _get_first_or_list(elements, first) |
| 235 | |
| 236 | def xpath(self, selector: str, *, clean: bool = False, first: bool = False, _encoding: str = None) -> _XPath: |
| 237 | """Given an XPath selector, returns a list of |
| 238 | :class:`Element <Element>` objects or a single one. |
| 239 | |
| 240 | :param selector: XPath Selector to use. |
| 241 | :param clean: Whether or not to sanitize the found HTML of ``<script>`` and ``<style>`` tags. |
| 242 | :param first: Whether or not to return just the first result. |
| 243 | :param _encoding: The encoding format. |
| 244 | |
| 245 | If a sub-selector is specified (e.g. ``//a/@href``), a simple |
| 246 | list of results is returned. |
| 247 | |
| 248 | See W3School's `XPath Examples |
| 249 | <https://www.w3schools.com/xml/xpath_examples.asp>`_ |
| 250 | for more details. |
| 251 | |
| 252 | If ``first`` is ``True``, only returns the first |
| 253 | :class:`Element <Element>` found. |
| 254 | """ |
| 255 | selected = self.lxml.xpath(selector) |
| 256 | |
| 257 | elements = [ |
| 258 | Element(element=selection, url=self.url, default_encoding=_encoding or self.encoding) |
| 259 | if not isinstance(selection, etree._ElementUnicodeResult) else str(selection) |
| 260 | for selection in selected |
| 261 | ] |
| 262 | |
| 263 | # Sanitize the found HTML. |
| 264 | if clean: |
| 265 | elements_copy = elements.copy() |
| 266 | elements = [] |
| 267 | |
| 268 | for element in elements_copy: |
| 269 | element.raw_html = lxml_html_tostring(cleaner.clean_html(element.lxml)) |
| 270 | elements.append(element) |
| 271 | |
| 272 | return _get_first_or_list(elements, first) |
| 273 | |
| 274 | def search(self, template: str) -> Result: |
| 275 | """Search the :class:`Element <Element>` for the given Parse template. |