MCPcopy
hub / github.com/psf/requests-html / find

Method find

requests_html.py:180–234  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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&#x27;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

Callers 15

genMethod · 0.95
base_urlMethod · 0.95
get_nextMethod · 0.80
test_class_seperationFunction · 0.80
test_css_selectorFunction · 0.80
test_containingFunction · 0.80
test_attrsFunction · 0.80
test_linksFunction · 0.80
test_async_linksFunction · 0.80
test_renderFunction · 0.80
test_async_renderFunction · 0.80
test_bare_renderFunction · 0.80

Calls 3

pqMethod · 0.95
ElementClass · 0.85
_get_first_or_listFunction · 0.85

Tested by 12

test_class_seperationFunction · 0.64
test_css_selectorFunction · 0.64
test_containingFunction · 0.64
test_attrsFunction · 0.64
test_linksFunction · 0.64
test_async_linksFunction · 0.64
test_renderFunction · 0.64
test_async_renderFunction · 0.64
test_bare_renderFunction · 0.64
test_bare_arenderFunction · 0.64
test_bare_js_evalFunction · 0.64
test_bare_js_async_evalFunction · 0.64