Pregunta

¿Cómo itero sobre los atributos HTML de un elemento Beautiful Soup?

Me gusta, dado:

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

Quiero " barra " y " bla " ;.

¿Fue útil?

Solución

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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top