如何迭代Beautiful Soup元素的HTML属性?

时间:2009-05-04 23:14:02

标签: python beautifulsoup

如何迭代Beautiful Soup元素的HTML属性?

喜欢,给定:

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

我想要“bar”和“blah”。

1 个答案:

答案 0 :(得分:32)

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