Jsoup获取元素旁边的数据

时间:2018-10-10 00:30:09

标签: java jsoup

我想获取hr标签旁边的数据。我如何用JSoup做到这一点?有没有一种方法可以让我遍历所有hr标签?

<HR>
Level: <B>Undergraduate</B><BR>
College or School: <B>College of Engineering and Applied Sciences</B><BR>
Department or Program: <B>Computer Science</B><BR>
<HR>
Level: <B>Undergraduate</B><BR>
College or School: <B>College of Engineering and Applied Sciences</B><BR>
Department or Program: <B>Computer Science</B><BR>
<HR>

我的期望值为

Level: <B>Undergraduate</B><BR>
College or School: <B>College of Engineering and Applied Sciences</B><BR>
Department or Program: <B>Computer Science</B><BR>

Level: <B>Undergraduate</B><BR>
College or School: <B>College of Engineering and Applied Sciences</B><BR>
Department or Program: <B>Computer Science</B><BR>

1 个答案:

答案 0 :(得分:0)

您可以使用document.getElementsByTag("hr")获得所有<hr>标签。

您可以从那里遍历返回的Elements,以获得每个单独的<hr>

要获取<hr>附近的元素,可以使用Element.nextSibling()获取相邻节点。