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

mik*_*ike 19 python beautifulsoup

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

喜欢,给定:

<foo bar="asdf" blah="123">xyz</foo>
Run Code Online (Sandbox Code Playgroud)

我想要"酒吧"和"等等".

Ric*_*dle 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
Run Code Online (Sandbox Code Playgroud)