javahtml

Is there an easy way to extract this table's information with java?


I want to extract this table's information, with all of its fields to create objects in java with my custom class. The object attributes are: Name (String), Id (int), Icon (A link, bitmap or image file), Quote (String), Description (String) and Quality (int). The purpose of this is to create a list and implement it in android studio. The website is https://bindingofisaacrebirth.fandom.com/wiki/Items

I tried many different things, but honestly I have no idea what I'm doing.


Solution

  • Here is a possible solution:

    public static void main(String[] args) throws Exception {
    
        String url = "https://bindingofisaacrebirth.fandom.com/wiki/Items";
        try{
            Document doc = Jsoup.connect(url).get();
            Elements table = doc.getElementsByTag("table");
            Elements row   = table.get(0).getElementsByClass("row-collectible");
            
            System.out.println("rows = " + row.size());
            
            for(int i=0; i<row.size(); i++){
                Elements cell  = row.get(i).getElementsByTag("td");
    
                String  name    = cell.get(0).text();
                String  id      = cell.get(1).text();
                String  link    = cell.get(2).getElementsByTag("a").get(0).getElementsByTag("img").attr("data-src");
    
                System.out.println("name: " + name);
                System.out.println("id:   " + id);
                System.out.println("link: " + link);
            }
    
        }catch (IOException e){
        }
    }
    

    Output:

    rows = 171
    ----------------
    name: A Pony
    id:   5.100.130
    link: https://static.wikia.nocookie.net/bindingofisaacre_gamepedia/images/1/15/Collectible_A_Pony_icon.png/revision/latest?cb=20210821050028
    ----------------
    name: Anarchist Cookbook
    id:   5.100.65
    link: https://static.wikia.nocookie.net/bindingofisaacre_gamepedia/images/c/c6/Collectible_Anarchist_Cookbook_icon.png/revision/latest?cb=20210821040839
    ----------------
    name: Best Friend
    id:   5.100.136
    link: https://static.wikia.nocookie.net/bindingofisaacre_gamepedia/images/a/af/Collectible_Best_Friend_icon.png/revision/latest?cb=20210821041224
    ----------------
    ...