Search code examples
javahtmlweb-scrapingjsoup

Jsoup how to get text from a specific column of an html table


I'm trying to print out all the locations in the table on this wikipedia page: https://en.wikipedia.org/wiki/COVID-19_pandemic, but it always shows up blank. Is this a problem with my code or am I searching for the wrong html classes?

            try {
                Document doc = Jsoup.connect("https://en.wikipedia.org/wiki/COVID-19_pandemic").get();
                for (Element row : doc.select("table.wikitable.plainrowheaders.sortable.jquery-tablesorter tr")){
                    if (row.select("th:nth-of-type(2)").text().equals("")){
                        continue;
                    }else {
                        final String location = row.select("th:nth-of-type(2)").text();
                        System.out.println(location);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

Solution

  • When I changed

    doc.select("table.wikitable.plainrowheaders.sortable.jquery-tablesorter tr")

    to

    doc.select("table.wikitable tr")

    I was able to get the countries names.

    Please try.