python newbie beautifulSoup question

Justin Ezequiel justin.mailinglists at gmail.com
Thu Apr 12 04:51:04 CEST 2007


On Apr 12, 4:15 am, Jon Crump <jjcr... at myuw.net> wrote:

> Is it possible to feed findAll() a list of tags WITH attributes?

>>> BeautifulSoup.__version__
'3.0.3'
>>> s = '''<x>\n<z>boo</z>\n<z a="foo" b="bar">hello</z>\n<y a="bar">boo</y>\n<y a="foo">hi</y>\n</x>'''
>>> soup = BeautifulSoup.BeautifulSoup(s)
>>> def func(tag):
...     if tag.name not in ('y', 'z'): return False
...     if tag.name=='y': return tag.get('a')=='foo'
...     return tag.get('a')=='foo' and tag.get('b')=='bar'
...
>>> soup.findAll(func)
[<z a="foo" b="bar">hello</z>, <y a="foo">hi</y>]
>>> def get_func(lst):
...     def func(tag):
...         for name, attrs in lst:
...             if tag.name==name:
...                 for k, v in attrs.items():
...                     if tag.get(k, None)==v: continue
...                     else: return False
...                 else: return True
...         else: return False
...     return func
...
>>> func2 = get_func([('y', {'a': 'foo'}), ('z', {'b': 'bar', 'a': 'foo'})])
>>> soup.findAll(func2)
[<z a="foo" b="bar">hello</z>, <y a="foo">hi</y>]
>>>





More information about the Python-list mailing list