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

如何用BeautifulSoup为不同层级h4元素的对应内容包裹section标签

用BeautifulSoup包裹嵌套层级不同的h4及后续内容为section标签

实现思路

核心是按文档顺序定位每个h4的区间范围,不管h4的嵌套深度,把每个h4到下一个h4之前的所有内容打包成section:

  1. 定位main元素,获取其中所有h4标签(包括嵌套在子元素内的)
  2. 遍历每个h4,确定当前section的结束边界:下一个h4(非最后一个h4)或main元素末尾(最后一个h4)
  3. 从当前h4开始,按文档顺序遍历后续节点,直到触及边界,收集所有节点
  4. 创建section标签,将收集到的节点迁移到section内,替换原位置的内容

代码实现

from bs4 import BeautifulSoup

# 示例HTML结构
html = """
<main>
  <div class="container">
    <h4>Section 1 Heading</h4>
    <p>Content for section 1.</p>
    <div>Another element in section 1.</div>
  </div>
  <h4>Section 2 Heading</h4>
  <ul>
    <li>List item in section 2.</li>
    <div>
      <h4>Section 3 Heading</h4>
      <p>Content for section 3, nested deeper.</p>
      <span>More content here.</span>
    </div>
  </ul>
</main>
"""

soup = BeautifulSoup(html, 'html.parser')
main = soup.find('main')
# 获取main下所有h4,按文档顺序排列
h4_list = main.find_all('h4')

for idx, current_h4 in enumerate(h4_list):
    # 确定当前section的结束节点
    next_h4 = h4_list[idx + 1] if idx < len(h4_list) - 1 else None
    
    # 收集当前section包含的所有节点
    section_nodes = []
    current_node = current_h4
    while current_node is not None:
        section_nodes.append(current_node)
        # 按文档顺序取下一个节点(跨层级)
        current_node = current_node.next_element
        # 遇到下一个h4则停止收集
        if current_node == next_h4:
            break
    
    # 创建section标签并插入到当前h4的原位置
    section_tag = soup.new_tag('section')
    current_h4.insert_before(section_tag)
    
    # 将收集的节点迁移到section内
    for node in section_nodes:
        if node.parent:
            node.extract()
        section_tag.append(node)

# 输出处理后的HTML
print(soup.prettify())

关键细节说明

  • 使用next_element而非next_sibling:前者会按文档顺序遍历所有后续节点,不受嵌套层级限制,完美适配h4在不同子元素中的场景
  • extract()方法:将节点从原DOM结构中移除,避免重复添加导致的结构混乱
  • 按索引遍历h4列表:清晰区分每个h4的区间边界,确保不会遗漏或重复包裹内容

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 19:55:31