summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeonard Richardson <leonardr@segfault.org>2013-05-08 12:13:07 -0400
committerLeonard Richardson <leonardr@segfault.org>2013-05-08 12:13:07 -0400
commit57a2cdfc271c1fb53516bb94bd2ebf44e689d55d (patch)
tree51c8b9fad396c7466dfa11e7f52b13171cb688a5
parentb225c0cedf7e17b9780df7cab99dd6cd0c0a88e7 (diff)
Give the checker the ability to stop the iteration over the generator by raising StopIteration.
-rw-r--r--bs4/element.py9
1 files changed, 8 insertions, 1 deletions
diff --git a/bs4/element.py b/bs4/element.py
index 6feced9..120471e 100644
--- a/bs4/element.py
+++ b/bs4/element.py
@@ -695,6 +695,8 @@ class PageElement(object):
self.count += 1
if self.count == self.destination:
return True
+ if self.count > self.destination:
+ raise StopIteration()
return False
checker = Counter(pseudo_value).nth_child_of_type
else:
@@ -786,7 +788,12 @@ class PageElement(object):
continue
if tag_name and candidate.name != tag_name:
continue
- if checker is None or checker(candidate):
+ if checker is not None:
+ try:
+ result = checker(candidate)
+ except StopIteration:
+ break
+ if checker is None or result:
if self._select_debug:
print " SUCCESS %s %s" % (candidate.name, repr(candidate.attrs))
new_context.append(candidate)