1

I am parsing tables using jsoup. I need to connect to division standing tables from this website: https://www.basketball-reference.com/leagues/NBA_2006.html. Don't know how to parse tables because I need to use the same method for every division standing table, but the id is different for older seasons (e.g. id="divs_standings_W", "id="divs_standings_E" and "id="divs_standings_"). Link to some older season: https://www.basketball-reference.com/leagues/NBA_1950.html.

How can I check if the table with the given id exists and if it exists put it in a variable table? Don't have much relevant code.

Document doc = Jsoup.connect("https://www.basketball-reference.com/leagues/NBA_1950.html").get();
Elements table = doc.select("table[id=\"divs_standings_\"]");
1
  • You can check if table != null - if true then it exists. Commented May 27, 2019 at 18:54

1 Answer 1

1

You can just use prefix matching. Use table[id^="divs_standings_"]. This will match all tables, with ids starting with divs_standings_:

Document doc = Jsoup.connect("https://www.basketball-reference.com/leagues/NBA_1950.html").get();
Element table = doc.selectFirst("table[id^=\"divs_standings_\"]");

This will work for old and new seasons.

To wrap this in a method you can just use something like this:

private static void processTable(String url) throws IOException {
    Document doc = Jsoup.connect(url).get();
    Element table = doc.selectFirst("table[id^=\"divs_standings_\"]");
    System.out.println(table);
}

and call it with both urls:

processTable("https://www.basketball-reference.com/leagues/NBA_1950.html");
processTable("https://www.basketball-reference.com/leagues/NBA_2006.html");

You also can use pattern matching if you have more complex ids. Check out the link above for this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.