-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexceptionalsoup.py
More file actions
35 lines (31 loc) · 1.31 KB
/
exceptionalsoup.py
File metadata and controls
35 lines (31 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""
A modified version of BeautifulSoup that adds a safe_find method which throws exceptions if nothing is found.
>>> html = "<a href='/index'>go to index</a>"
>>> bs = ExceptionalSoup(html)
>>> bs.safe_find(name='a', attrs={'href': '/index'})
<a href="/index">go to index</a>
>>> bs.safe_find(name='b')
Traceback (most recent call last):
...
ExceptionalSoupError: expected 1 result but got 0 for safe_find(name=b, attrs={}, recursive=True, text=None, kwargs={})
<BLANKLINE>
=Soup Contents Follows=
<a href="/index">go to index</a>
"""
from BeautifulSoup import BeautifulSoup as _BeautifulSoup
class ExceptionalSoupError(RuntimeError):
pass
class ExceptionalSoup(_BeautifulSoup):
def safe_find(self, name=None, attrs={}, recursive=True, text=None, **kwargs):
result = self.findAll(name, attrs, recursive, text, **kwargs)
if len(result) != 1:
errormsg = 'expected 1 result but got %s for safe_find(name=%s, attrs=%s, recursive=%s, text=%s, kwargs=%s)' % (len(result), name, attrs, recursive, text, kwargs)
errormsg += '\n\n=Soup Contents Follows=\n' + str(self)
e = ExceptionalSoupError(errormsg)
raise e
return result[0]
del _BeautifulSoup
if __name__ == '__main__':
import doctest
print "Running doctest . . ."
doctest.testmod()