gpt4 book ai didi

java - 如何迭代jsoup中的各个元素?

转载 作者:行者123 更新时间:2023-12-01 11:13:21 25 4
gpt4 key购买 nike

我必须通过 jsoup 解析页面。该页面有一个类和各种带有 ph1h2h3 等标签的元素。我想要将它们一一解析,然后逐一处理。页面如下所示:

    <div class="pf-content">
<p>For centuries, Spain shone and progressed under Muslim rule. Unfortunately, the city of Seville fell prey to the barbaric onslaught of the Kingdom of Castile in the year 1248. Several innocent Spaniards were killed, many were forced to leave their homeland and seek refuge elsewhere, whereas many others were captured and taken as slaves. The rulers of Castile further destroyed remnants of Islamic life and culture, <a href="https://muslimmemo.com/masjids-spain/">including masjids</a>.</p>
<h3>Original Arabic Text</h3>
<h4>Original Arabic Text</h4>
</div>

p、h3、h4 等出现的顺序很重要,因为我必须将其解析为 android textview。

我能做的是:

Document document = Jsoup.connect("page link here").get();

Elements pTag = document.select("div.pf-content");

但是我应该如何继续呢?请帮助我。

我尝试的是:

Elements elements = document.select("div.pf-content");

for (Element element : elements) {
Log.d("FullContent", "elements are: " + element);
if (element.select("p").first() != null) {
Log.d("FullContent", "a p tag");
if (element.select("p").first().select("img").first() != null) {
Log.d("FullContent", "the tag " + "has src");
}


} else if (element.select("h1").first() != null) {
Log.d("FullContent", "a h1 tag");
} else if (element.select("h2").first() != null) {
Log.d("FullContent", "a h2 tag");
} else if (element.select("h3").first() != null) {
Log.d("FullContent", "a h3 tag");
} else if (element.select("h4").first() != null) {
Log.d("FullContent", "a h4 tag");
} else {
Log.d("FullContent", "other tag");
}

}

最佳答案

获得使用 Elements pTag = document.select("div.pf-content"); 找到的 Elements 后,您可以执行以下操作:

元素 elements = pTag.first().children();
for (元素 e : 元素){
//对每个元素做一些事情
}

关于java - 如何迭代jsoup中的各个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32120788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com