Search code examples
javajsoup

How to get 1 element from a table based on data-stat


Page URL: https://www.basketball-reference.com/players/c/collijo01.html

Table location: <tr id="per_game.2019 class="full_table">

Element I want: <td class="right" data-stat="fg3a_per_g">2.5</td>

With the 2.5 being what I want. I used to grab the entire table then cycle to this information, but I have found this to be more work than trying to grab based on the data-stat name. However so far I have failed

Question is: Can jsoup grab info based on data-stat section?

Code:

public static void getDataTest(String url) throws IOException
{
    String html = Jsoup.connect(url).execute().body();
    html = html.replaceAll("<!--", "");
    html = html.replaceAll("-->", "");
    Document doc = Jsoup.parse(html);
    Element tableElements = doc.getElementById("table#fg3a_per_g");

    System.out.print("Element found was: " + tableElements);
}

Note I've also tried "fg3a_per_g" by itself without table


Solution

  • Can jsoup grab info based on data-stat section?

    Sure:

    Element element = doc.select("td[data-stat=fg3a_per_g]").first();
    

    What you call section is actually called attribute. Also take a look at more CSS selectors. You can't use all of them with Jsoup, but the basic ones are supported and very useful: https://www.w3schools.com/cssref/css_selectors.asp