mirror of
https://github.com/kennethreitz/requests-html.git
synced 2026-06-05 14:50:20 +00:00
merge master
This commit is contained in:
+19
-5
@@ -103,7 +103,6 @@ class BaseParser:
|
||||
|
||||
See W3School's `CSS Selectors Reference <https://www.w3schools.com/cssref/css_selectors.asp>`_ for more details.
|
||||
|
||||
|
||||
If ``first`` is ``True``, only returns the first :class:`Element <Element>` found."""
|
||||
|
||||
encoding = _encoding or self.encoding
|
||||
@@ -115,12 +114,27 @@ class BaseParser:
|
||||
return _get_first_or_list(elements, first)
|
||||
|
||||
def xpath(self, selector: str, first: bool = False, _encoding: str = None):
|
||||
"""Given an XPath selector, returns a list of :class:`Element <Element>` objects.
|
||||
"""Given an XPath selector, returns a list of
|
||||
:class:`Element <Element>` objects.
|
||||
|
||||
See W3School's `XPath Examples <https://www.w3schools.com/xml/xpath_examples.asp>`_ for more details.
|
||||
If a sub-selector is specified (e.g. ``//a/@href``), a simple
|
||||
list of results is returned.
|
||||
|
||||
If ``first`` is ``True``, only returns the first :class:`Element <Element>` found."""
|
||||
c = [Element(element=e, url=self.url, default_encoding=_encoding or self.encoding) for e in self.lxml.xpath(selector)]
|
||||
See W3School's `XPath Examples
|
||||
<https://www.w3schools.com/xml/xpath_examples.asp>`_
|
||||
for more details.
|
||||
|
||||
If ``first`` is ``True``, only returns the first
|
||||
:class:`Element <Element>` found.
|
||||
"""
|
||||
selected = self.lxml.xpath(selector)
|
||||
c = []
|
||||
for selection in selected:
|
||||
if not isinstance(selection, etree._ElementUnicodeResult):
|
||||
element = Element(element=selection, url=self.url, default_encoding=_encoding or self.encoding)
|
||||
else:
|
||||
element = selection
|
||||
c.append(element)
|
||||
|
||||
return _get_first_or_list(c, first)
|
||||
|
||||
|
||||
@@ -59,6 +59,9 @@ def test_xpath():
|
||||
html = r.html.xpath('/html', first=True)
|
||||
assert 'no-js' in html.attrs['class']
|
||||
|
||||
a_hrefs = r.html.xpath('//a/@href')
|
||||
assert '#site-map' in a_hrefs
|
||||
|
||||
|
||||
def test_html_loading():
|
||||
doc = """<a href='https://httpbin.org'>"""
|
||||
@@ -77,4 +80,4 @@ def test_anchor_links():
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_anchor_links()
|
||||
test_xpath()
|
||||
|
||||
Reference in New Issue
Block a user