Beautiful Soup如何提取类属性值?

时间:2018-12-29 11:20:40

标签: python beautifulsoup

我使用beautifulsoup提取了类的多个属性值,但是['fa', 'fa-address-book-o']不是我想要的结果。

from bs4 import BeautifulSoup

html = "<i class='fa fa-address-book-o' aria-hidden='true'></i>"

soup = BeautifulSoup(html, "lxml")

h2 = soup.select("i")

print(h2[0]['class'])

我希望效果如下:

fa fa-address-book-o

1 个答案:

答案 0 :(得分:0)

加入列表中的所有元素,并在它们之间放置一个空格

from bs4 import BeautifulSoup

html = "<i class='fa fa-address-book-o' aria-hidden='true'></i>"

soup = BeautifulSoup(html, "lxml")

h2 = soup.select("i")

print(' '.join(h2[0]['class']))