Given a CSS Selector, returns a list of :class:`Element ` objects or a single one. :param selector: CSS Selector to use. :param clean: Whether or not to sanitize the found HTML of `` `` and `` `` tags. :param containing: If specified, only retur
(self, selector: str = "*", *, containing: _Containing = None, clean: bool = False, first: bool = False, _encoding: str = None)
| 178 | return self.lxml.text_content() |
| 179 | |
| 180 | def find(self, selector: str = "*", *, containing: _Containing = None, clean: bool = False, first: bool = False, _encoding: str = None) -> _Find: |
| 181 | """Given a CSS Selector, returns a list of |
| 182 | :class:`Element <Element>` objects or a single one. |
| 183 | |
| 184 | :param selector: CSS Selector to use. |
| 185 | :param clean: Whether or not to sanitize the found HTML of ``<script>`` and ``<style>`` tags. |
| 186 | :param containing: If specified, only return elements that contain the provided text. |
| 187 | :param first: Whether or not to return just the first result. |
| 188 | :param _encoding: The encoding format. |
| 189 | |
| 190 | Example CSS Selectors: |
| 191 | |
| 192 | - ``a`` |
| 193 | - ``a.someClass`` |
| 194 | - ``a#someID`` |
| 195 | - ``a[target=_blank]`` |
| 196 | |
| 197 | See W3School's `CSS Selectors Reference |
| 198 | <https://www.w3schools.com/cssref/css_selectors.asp>`_ |
| 199 | for more details. |
| 200 | |
| 201 | If ``first`` is ``True``, only returns the first |
| 202 | :class:`Element <Element>` found. |
| 203 | """ |
| 204 | |
| 205 | # Convert a single containing into a list. |
| 206 | if isinstance(containing, str): |
| 207 | containing = [containing] |
| 208 | |
| 209 | encoding = _encoding or self.encoding |
| 210 | elements = [ |
| 211 | Element(element=found, url=self.url, default_encoding=encoding) |
| 212 | for found in self.pq(selector) |
| 213 | ] |
| 214 | |
| 215 | if containing: |
| 216 | elements_copy = elements.copy() |
| 217 | elements = [] |
| 218 | |
| 219 | for element in elements_copy: |
| 220 | if any([c.lower() in element.full_text.lower() for c in containing]): |
| 221 | elements.append(element) |
| 222 | |
| 223 | elements.reverse() |
| 224 | |
| 225 | # Sanitize the found HTML. |
| 226 | if clean: |
| 227 | elements_copy = elements.copy() |
| 228 | elements = [] |
| 229 | |
| 230 | for element in elements_copy: |
| 231 | element.raw_html = lxml_html_tostring(cleaner.clean_html(element.lxml)) |
| 232 | elements.append(element) |
| 233 | |
| 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 |