class='something' as kwarg

George Sakkis george.sakkis at gmail.com
Sat Nov 17 16:13:03 CET 2007


On Nov 17, 3:45 am, "Vladimir Rusinov" <vladi... at greenmice.info>
wrote:
> Hello!
>
> I'm using beautiful soup html parser, and I need to get all '<div
> class=g>...</div>' tags.
> It can be done by:
>
> import BeautifulSoup as BSoup
>
> ...
>
> soup = BSoup(page)
> for div in soup.findAll('div', class='g'):
>     <do something>
>
> But how can I use `class` as kwarg name?

"""
You could search by CSS class with soup.find("tagName", { "class" :
"cssClass" }), but that's a lot of code for such a common operation.
Instead, you can pass a string for attrs instead of a dictionary. The
string will be used to restrict the CSS class.
"""

http://www.crummy.com/software/BeautifulSoup/documentation.html#Searching%20by%20CSS%20class


George




More information about the Python-list mailing list