views:

510

answers:

1

How do I iterate over the HTML attributes of a Beautiful Soup element?

Like, given:

<foo bar="asdf" blah="123">xyz</foo>

I want "bar" and "blah".

+10  A: 
from BeautifulSoup import BeautifulSoup
page = BeautifulSoup('<foo bar="asdf" blah="123">xyz</foo>')
for attr, value in page.find('foo').attrs:
    print attr, "=", value

# Prints:
# bar = asdf
# blah = 123
RichieHindle