Search code examples
javajsoup

Why is the loop not executed in Jsoup?


I'm trying to do a web scraper for the league of legend's page. I'm trying to first get a list of all champions from here http://gameinfo.euw.leagueoflegends.com/en/game-info/champions/ .

However, I can't figure out why is my loop not working.

Here is my code:

    Document doc = Jsoup.connect("http://gameinfo.euw.leagueoflegends.com/en/game-info/champions").get();
Elements span = doc.select("div#champion-grid-container > div.content-border > div#champion-grid-content > div.rg-box-container rg-display-Riot.champions.GridView > ul > li");
if(span != null){
    System.out.println("The class grid exist!!");
    Elements lista = span.select("li#champion-grid-aatrox");
    if(lista != null){
        System.out.println("li#champion-grid-aatrox Exist!!");
    }else{
        System.out.println("Nop :(");
    }
    Elements maidep = lista.select("div.champ-name");
    if(maidep != null){
        System.out.println("div.champ-name Exist!!");
    }else{
        System.out.println("Nop :(");
    }
    Elements maidep2 = maidep.select("a[href]");
    if(maidep2 != null){
        System.out.println("a Exist!!");
    }else{
        System.out.println("Nop :(");
    }
    for(Element nuj : maidep2)
    System.out.println("Content is " + nuj.text());
}
else {
    System.out.println("Class Grid Nop:(");
}

I know it's a bad practice to select divs like that, at first I tried to go all the way in one select to that particular element, but it was not returning anything so I wanted to go through each div/parent until I got there and see where it gets lost. The output is this:

The class grid exist!!
li#champion-grid-aatrox Exist!!
div.champ-name Exist!!
a Exist!!

So the "Content is" message is not even displayed.


Solution

  • According to JSoup doc, Elements.select()

    returns the filtered list of elements, or an empty list if none match.

    therefore checking if the Element is null is not enough to tell if your selector matched any elements. You should instead be checking if elements.size() > 0, and you're likely to discover that one of your selectors hasn't matched anything.

    You can use a this site to try your selectors in real time and save some time.