PhilDpt PhilDpt - 6 months ago 13
HTML Question

Get text from span using Jsoup (java)

Hello I would like to get the value following "cotation" but I don't know how to do it.

<span id="brs-sl57350199c5a53">
<span class="cotation">16.33 (c)</span>
</span>


I'm using Jsoup and this my code:

Document doc = Jsoup.connect("http://www.boursorama.com/bourse/actions/cours_az.phtml").get();

Element loginform = doc.getElementById("content"); //brs-sl5734ff406037d
Elements inputElements = loginform.getElementsByTag("span");

for (Element inputElement : inputElements) {
String key = inputElement.attr("class");
System.out.println("Param name: "+ key);
}


And this is what I have:

Param name: cotation


Do you have any idea to be able to get the value instead of "cotation"?

Thanks in advance.

Answer

Add this in your loop :

if ("cotation".equals(key)) {
    System.out.println(inputElement.html());
}

And you will get :

16.33 (c)