Reputation: 15
I want to extract the value for specefic titles in the table such as;
<tr>
<th colspan="8">
<a href="/wiki/Hit_points" title="Hit points" class="mw-redirect">Hit points</a>
</th>
<td colspan="12"> 240</td>
</tr>
<tr>
<th colspan="8"> <a href="/wiki/Aggressive" title="Aggressive" class="mw-redirect">Aggressive</a>
</th><td colspan="12"> Yes
</td></tr>
I want to be able to get the value for example;
if title equals "Hit points" returns 240
in the above case.
package test;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Attribute;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class topkek {
public static void main(String[] args) {
try {
Response res = Jsoup.connect("http://2007.runescape.wikia.com/wiki/King_black_dragon").execute();
String html = res.body();
Document doc2 = Jsoup.parseBodyFragment(html);
Element body = doc2.body();
Elements tables = body.getElementsByTag("table");
for (Element table : tables) {
if (table.className().contains("infobox")==true) {
System.out.println(table.getElementsByAttribute("title").text());
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Upvotes: 1
Views: 2106
Reputation: 2566
No need to go through the document manually, you can simply use a selector for this:
response
.parse()
.select("th:has(a[title=\"Hit points\"]) ~ td")
.text()
This selects a th
element that has a nested a
with the title and has a sibling td
element from which you can read the content using text()
See here for syntax details and here for an online sandbox.
Edit: if you want to list multiple elements, you can use something like this:
document
.select("th:has(a[title])")
.forEach(e -> {
System.out.println(e.text());
System.out.println(((Element) e.nextSibling()).text());
});
Upvotes: 1