From fae210c8a27a6a81ce487927c4f243b4a561b1c6 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Sun, 25 Feb 2018 09:44:45 -0500 Subject: [PATCH] improvements Signed-off-by: Kenneth Reitz --- README.rst | 9 +++++---- requests_html.py | 24 ++++++++++++++++++++---- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/README.rst b/README.rst index d73fef8..0b6fc1a 100644 --- a/README.rst +++ b/README.rst @@ -45,7 +45,7 @@ Select an element with a jQuery selector. .. code-block:: pycon - >>> about = r.html.find('#about')[0] + >>> about = r.html.find('#about', first=True) Grab an element's text contents: @@ -85,11 +85,11 @@ Render an Element as Markdown: * [Getting Started](/about/gettingstarted/) * [Help](/about/help/) * [Python Brochure](http://brochure.getpython.info/) - + Search for text on the page: - + .. code-block:: pycon - + >>> r.html.search('Python is a {} language')[0] programming @@ -97,6 +97,7 @@ More complex CSS Selector example: >>> r = session.get('https://github.com/') >>> sel = 'body > div.application-main > div.jumbotron.jumbotron-codelines > div > div > div.col-md-7.text-center.text-md-left > p' + >>> print(r.html.find(sel)[0].text) GitHub is a development platform inspired by the way you work. From open source to business, you can host and review code, manage projects, and build software alongside millions of other developers. diff --git a/requests_html.py b/requests_html.py index 5135697..71c2da8 100644 --- a/requests_html.py +++ b/requests_html.py @@ -63,13 +63,21 @@ class Element: """HTML representation of the element.""" return etree.tostring(self.element).decode('utf-8').strip() - def find(self, selector): + def find(self, selector, first=False): """Given a jQuery selector, returns a list of element objects.""" def gen(): for found in self.pq(selector): yield Element(found) - return [g for g in gen()] + c = [g for g in gen()] + + if first: + try: + return c[0] + except IndexError: + return None + else: + return c def xpath(self, selector): """Given an XPath selector, returns a list of element objects.""" @@ -96,13 +104,21 @@ class HTML: def __repr__(self): return "".format(repr(self.url)) - def find(self, selector): + def find(self, selector, first=False): """Given a jQuery selector, returns a list of element objects.""" def gen(): for found in self.pq(selector): yield Element(found) - return [g for g in gen()] + c = [g for g in gen()] + + if first: + try: + return c[0] + except IndexError: + return None + else: + return c def search(self, template): """Searches the page for the given parse template."""