python - Beautiful Soup 如何提取类属性值?
问题描述
我使用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
解决方案
加入列表中的所有元素,并在它们之间放置一个空格
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']))
推荐阅读
- python - 使用 dataframe.at 时浮点数被舍入,而不是 dataframe.iloc
- javascript - setInterval 和 setTimeout 中的间隔不准确
- asciidoc - 如何为现有的 Asciidoctor Asciidoc 宏创建自定义 HTML 输出?
- java - 来自 SQL 数据库的带有时间的 JFreechart 图表
- node.js - 尝试在 Node.js 上运行项目时出错
- javascript - 用于解码 PDF 流的 JavaScript 库
- python - 如何在python中增加样本日期
- amazon-web-services - Azure Devops 服务和 VPN - 加密数据
- android-espresso - 如何从 Espresso DataInteraction 获取列表大小
- windows - 一个 - 并且只有一个 - 网络文件夹的访问问题