繁体   English   中英

如何用Jsoup读取h3和文本元素?

[英]How can I read h3 and after text element with Jsoup?

我想读<h3>之间的文本<h3>所以我想创建一个JSON模式类似标题:文本,文本,文本H3和文本而不广告。

{
  "title": "text,text,text",
  "title": "text",
  "title": "text",
  ...
}

在这种情况下,如何使用Java或Kotlin进行此操作?

<div class="biri" id="biri">
    <h1>Yoksa Birisi mi itti?</h1>
    <h3>Title</h3>Text,
    <br>Text,
    <br>Text.
    <h3>Title:</h3>Text
    <h3>Title:</h3>Text
    <div class="ad">
        <div style="max-width:336px;">
            <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
            <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-7180771993103993" data-ad-slot="2897611612" data-ad-format="auto"></ins>
            <script>
                (adsbygoogle = window.adsbygoogle || []).push({});
            </script>
        </div>
    </div>
    <h3>Title</h3>Text:
    <b>Text:</b> (Text
    <br>
</div>

您可以使用Document.select()获取所有h3标记:

Document doc = Jsoup.parse(html);
List<String> h3s = doc.select("h3").stream()
        .map(Element::text)
        .collect(Collectors.toList());

这将提取所有h3标签的内容并收集它们的内容。 结果是这样的:

[Title, Title:, Title:, Title]

除了JSON之外,您要创建的JSON无效,因为JSON对象中的键必须是唯一的,因此您不能拥有多个h3键。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM