Skip to content

Commit

Permalink
xpath improvements
Browse files Browse the repository at this point in the history
Signed-off-by: Kenneth Reitz <me@kennethreitz.org>
  • Loading branch information
kennethreitz committed Feb 28, 2018
1 parent 65f6271 commit 495b0c3
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions requests_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,27 @@ def find(self, selector: str, first: bool = False, _encoding: str = None):
return elements

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.
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)
try:
c = [Element(element=e, url=self.url, default_encoding=_encoding or self.encoding) for e in selected]
# Sanity check.
[e.keys for e in c]
except AttributeError:
c = selected

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)]
if first:
try:
return c[0]
Expand Down

0 comments on commit 495b0c3

Please sign in to comment.