小编典典
如果要获取和处理两个连续h1标签之间的所有元素,则可以处理同级对象。这是一些示例代码:
public static void h1s() {
String html = "" +
"
" +"
" +"
title 1
" +"
hello 1
" +"
"
" +"
hello" +"
world" +"
1" +"
" +"
" +"
title 2
" +"
hello 2
" +"
"
" +"
hello" +"
world" +"
2" +"
" +"
" +"
title 3
" +"
hello 3
" +"
"
" +"
hello" +"
world" +"
3" +"
" +"
" +"" +
"";
Document doc = Jsoup.parse(html);
Element firstH1 = doc.select("h1").first();
Elements siblings = firstH1.siblingElements();
List elementsBetween = new ArrayList();
for (int i = 1; i < siblings.size(); i++) {
Element sibling = siblings.get(i);
if (! "h1".equals(sibling.tagName()))
elementsBetween.add(sibling);
else {
processElementsBetween(elementsBetween);
elementsBetween.clear();
}
}
if (! elementsBetween.isEmpty())
processElementsBetween(elementsBetween);
}
private static void processElementsBetween(
List elementsBetween) {
System.out.println("---");
for (Element element : elementsBetween) {
System.out.println(element);
}
}
2020-09-23
该代码示例展示了如何使用Jsoup库在Java中解析HTML,获取两个连续h1标签之间的所有元素。通过遍历并检查元素的标签名,筛选出非h1的同级元素进行处理。
2125

被折叠的 条评论
为什么被折叠?



