如何迭代Beautiful Soup元素的HTML属性?
喜欢,给定:
<foo bar="asdf" blah="123">xyz</foo>
我想要“bar”和“blah”。
答案 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