From 92fc86a033e74ffe5805448e4c88b621b033f4a4 Mon Sep 17 00:00:00 2001 From: Laurent Bachelier Date: Tue, 12 Apr 2011 01:00:28 +0200 Subject: [PATCH] Add support for xpath in LxmlHtmlParser.select The returned results are similar to those of the cssselect method so there wasn't much to do except calling it. --- weboob/tools/parsers/lxmlparser.py | 40 ++++++++++++++++-------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/weboob/tools/parsers/lxmlparser.py b/weboob/tools/parsers/lxmlparser.py index 84dc6a11..1430f798 100644 --- a/weboob/tools/parsers/lxmlparser.py +++ b/weboob/tools/parsers/lxmlparser.py @@ -60,23 +60,25 @@ class LxmlHtmlParser(IParser): """ if method == 'cssselect': results = element.cssselect(selector) - if nb is None: - return results - elif isinstance(nb, basestring) and nb == 'many': - if results is None or len(results) == 0: - raise BrokenPageError('Element not found with selector "%s"' % selector) - elif len(results) == 1: - raise BrokenPageError('Only one element found with selector "%s"' % selector) - else: - return results - elif isinstance(nb, int) and nb > 0: - if results is None: - raise BrokenPageError('Element not found with selector "%s"' % selector) - elif len(results) < nb: - raise BrokenPageError('Not enough elements found (%d expected) with selector "%s"' % (nb, selector)) - else: - return results[0] if nb == 1 else results - else: - raise Exception('Unhandled value for kwarg "nb": %s' % nb) + elif method == 'xpath': + results = element.xpath(selector) else: - raise NotImplementedError('Only cssselect method is implemented for the moment') + raise NotImplementedError('Only the cssselect and xpath methods are supported') + if nb is None: + return results + elif isinstance(nb, basestring) and nb == 'many': + if results is None or len(results) == 0: + raise BrokenPageError('Element not found with selector "%s"' % selector) + elif len(results) == 1: + raise BrokenPageError('Only one element found with selector "%s"' % selector) + else: + return results + elif isinstance(nb, int) and nb > 0: + if results is None: + raise BrokenPageError('Element not found with selector "%s"' % selector) + elif len(results) < nb: + raise BrokenPageError('Not enough elements found (%d expected) with selector "%s"' % (nb, selector)) + else: + return results[0] if nb == 1 else results + else: + raise Exception('Unhandled value for kwarg "nb": %s' % nb)