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

如何用Jsoup获取h2标签之后及之间的所有p标签

问题描述

给定如下HTML结构:

<h2 id="17273">bla bla bla 1</h2>
<p>Text i need</p>
<p>Text i need</p>
<p>Text i need</p>
<h2 id="45626">bla bla bla 2</h2>
<p>Text i need</p>
<p>Text i need</p>
<p>Text i need</p>
<h2 id="78519">bla bla bla 3</h2>
<p>Text i need</p>
<p>Text i need</p>
<h2 id="72725">bla bla bla 2</h2>
<p>Text i need</p>
<p>Text i need</p>

需要提取每个<h2>标签之后、下一个<h2>之前的所有<p>标签,建立如下映射关系:

[(h2 with id 17273 = all p tags below it), (h2 with id 45626 = all p tags below it), ...]

尝试过siblingElements等方法但未成功,寻求可行方案。

解决方案

方法一:Python + BeautifulSoup

核心逻辑是遍历所有<h2>标签,逐个收集后续兄弟节点,直到遇到下一个<h2>停止,仅保留<p>标签内容。

from bs4 import BeautifulSoup

html = """
<h2 id="17273">bla bla bla 1</h2>
<p>Text i need</p>
<p>Text i need</p>
<p>Text i need</p>
<h2 id="45626">bla bla bla 2</h2>
<p>Text i need</p>
<p>Text i need</p>
<p>Text i need</p>
<h2 id="78519">bla bla bla 3</h2>
<p>Text i need</p>
<p>Text i need</p>
<h2 id="72725">bla bla bla 2</h2>
<p>Text i need</p>
<p>Text i need</p>
"""

soup = BeautifulSoup(html, 'html.parser')
result = []

for h2 in soup.find_all('h2'):
    p_content = []
    # 遍历当前h2的所有后续兄弟节点
    for sibling in h2.next_siblings:
        if sibling.name == 'h2':
            break
        if sibling.name == 'p':
            p_content.append(sibling.get_text(strip=True))
    result.append((f"h2 with id {h2['id']}", p_content))

print(result)

运行后输出结构:

[
    ("h2 with id 17273", ["Text i need", "Text i need", "Text i need"]),
    ("h2 with id 45626", ["Text i need", "Text i need", "Text i need"]),
    ("h2 with id 78519", ["Text i need", "Text i need"]),
    ("h2 with id 72725", ["Text i need", "Text i need"])
]

方法二:JavaScript + Cheerio(Node.js环境)

通过遍历<h2>节点,逐个获取后续兄弟元素,直到遇到新的<h2>为止,收集对应<p>文本。

const cheerio = require('cheerio');

const html = `
<h2 id="17273">bla bla bla 1</h2>
<p>Text i need</p>
<p>Text i need</p>
<p>Text i need</p>
<h2 id="45626">bla bla bla 2</h2>
<p>Text i need</p>
<p>Text i need</p>
<p>Text i need</p>
<h2 id="78519">bla bla bla 3</h2>
<p>Text i need</p>
<p>Text i need</p>
<h2 id="72725">bla bla bla 2</h2>
<p>Text i need</p>
<p>Text i need</p>
`;

const $ = cheerio.load(html);
const result = [];

$('h2').each((_, h2El) => {
    const h2Id = $(h2El).attr('id');
    const pTexts = [];
    let nextSibling = $(h2El).next();
    while (nextSibling.length > 0) {
        if (nextSibling.is('h2')) break;
        if (nextSibling.is('p')) pTexts.push(nextSibling.text().trim());
        nextSibling = nextSibling.next();
    }
    result.push([`h2 with id ${h2Id}`, pTexts]);
});

console.log(result);

内容的提问来源于stack exchange,提问作者Stɑrry Shivɑm

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 15:21:01