You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用BeautifulSoup与Python将段落内图标span转换为文本

问题描述

现有包含内联span图标元素的段落,代码结构如下:

<p>
  <span class="icon-water icon"></span>
  Some text...
  <span class="icon-steel icon"></span>
  some more text.
</p>

需要编写Python函数提取该段落文本,并将图标span转换为对应文本:

<span class="icon-water icon"></span> => '{water}'
<span class="icon-steel icon"></span> => '{steel}'

最终需得到结果:

{water} Some text... {steel} some more text.

请问如何通过BeautifulSoup实现此需求?


解决方案

可以通过遍历目标<p>标签下的所有子节点,区分文本节点和图标span节点,分别处理后拼接内容来实现:

  1. 先安装beautifulsoup4库:pip install beautifulsoup4
  2. 解析HTML内容并定位到目标<p>标签
  3. 逐个处理<p>下的子节点:
    • 文本节点:提取内容并清理首尾空白,跳过空文本避免多余空格
    • span图标节点:从class属性中提取图标名称,格式化为{图标名}的形式
  4. 拼接所有处理后的片段,整理成整洁的最终文本

完整代码示例

from bs4 import BeautifulSoup, NavigableString

def convert_icon_span_to_text(html):
    soup = BeautifulSoup(html, 'html.parser')
    p_element = soup.find('p')
    output_parts = []
    
    for node in p_element.contents:
        # 处理纯文本节点
        if isinstance(node, NavigableString):
            cleaned_text = node.strip()
            if cleaned_text:
                output_parts.append(cleaned_text)
        # 处理图标span节点
        elif node.name == 'span' and 'icon' in node.get('class', []):
            # 提取图标名称(从icon-开头的类名中拆分)
            for cls in node.get('class'):
                if cls.startswith('icon-'):
                    icon_name = cls.split('icon-')[1]
                    output_parts.append(f'{{{icon_name}}}')
                    break
    
    # 用单个空格拼接所有片段,保证格式规范
    return ' '.join(output_parts)

# 测试代码
test_html = '''
<p>
  <span class="icon-water icon"></span>
  Some text...
  <span class="icon-steel icon"></span>
  some more text.
</p>
'''

print(convert_icon_span_to_text(test_html))
# 输出结果:{water} Some text... {steel} some more text.

代码说明

  • NavigableString是BeautifulSoup中文本节点的类型,用来区分标签和纯文本内容
  • 遍历span的class属性筛选icon-开头的类名,确保不管class顺序如何都能正确提取图标名称
  • 清理文本节点的空白并跳过空内容,避免原HTML的换行、缩进产生多余空格
  • 最后用' '.join()拼接,保证片段之间只有一个空格,符合预期格式

内容的提问来源于stack exchange,提问作者kevski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 20:10:48