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

时间:2021-08-20 20:33:53

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

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

Like, given:

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

I want "bar" and "blah".

我想要“酒吧”和“等等”。

1 个解决方案

#1


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

#1


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