简体   繁体   中英

Jsoup get value if contains attribute

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();
        }
    }

}

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());
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM