python newbie beautifulSoup question

Jon Crump jjcrump at myuw.net
Thu Apr 12 12:24:05 EDT 2007


Justin,

Wow! I thought there might be a 'simple' way! thanks so much, you've given 
me a lot to chew on.

Jon
__________
J.J. Crump
Dept. of History 353560
University of Washington
Seattle, WA. 98195


On Wed, 11 Apr 2007, Justin Ezequiel wrote:

> 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>]
>>>>
>
>
> -- 
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list