小编典典
如果要获取和处理两个连续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