[英]Traversing a sibling node in JSOUP based on a search text
考慮以下來自URL的HTML。 我需要首先對文本“ Student 1”進行搜索,然后選擇對應的學校,在這種情況下為“ MIT School”。 如何在JSOUP中做到這一點?
<table>
<tbody>
<tr>
<td valign="top">
<div style="border-width:1px;border-color:#cccccc;border-style:solid;">
<table bordercolor="#483D8B">
<tbody>
<tr>
<th colspan="2" bgcolor="#483D8B" height="25"><font face="Verdana" size="2" color="white">MIT School</font></th>
</tr>
<tr>
<td width="120" height="15"><font face="Arial" size="2" color="black"> <b>Student 1</b> </font></td>
</tr>
</tbody>
</table>
到目前為止,我只能成功搜索文本。
System.out.println("This is :"+Jsoup.parse(url, timeout)
.select("b:containsOwn(Student 1");
我得到的輸出是
<b>This is :Student 1</b>
我在網上找不到許多有關JSOUP的示例。 這有什么幫助嗎?
我認為每個學生都有一張專用的桌子。 然后,您可以嘗試以下操作:
Element sibling = doc.select("b:containsOwn(Student 1)")
.first().parent().parent().parent().firstElementSibling();
System.out.println(sibling.select("th").text());
注意,在這種情況下,我們僅考慮第一個結果。 您必須迭代包含“學生1”的所有b元素,以便獲取所需的所有數據點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.