Obtener información de un HTML con Jsoup

Para una aplicación de bolsa necesitaba recabar información sobre unos sitios web. despues de unas indagaciones me he decantado por Jsoup por su simplicidad.

Mostrare como lo he utilizado para obtener el beneficio y el dividendo por acción.
Primero el código HTML:

<tr>
<td class="item">Beneficio por acción</td>
<td align='center'>1,0900 EUR</td>
<td align='center'>1,2188 EUR</td>
<td align='center'>1,3690 EUR</td>
</tr>
<tr>
<td class="item">Dividendo por acción</td>
<td align='center'>0,3700 EUR</td>
<td align='center'>0,4200 EUR</td>
<td align='center'>0,4718 EUR</td>
</tr>

Y ahora el código Java:

try {
  Document doc = Jsoup.connect(url).get();
  Elements items = doc.select(".item");
  for (Element item : items) {
    if (item.text().indexOf("Ben") > -1) {
       Elements bens = item.nextElementSibling().select("td");
       for (Element ben : bens) {
          ratio = ratio + " " + getFirstNum(ben.text());
       }
     }
     if (item.text().indexOf("Div") > -1) {
	Elements divs = item.nextElementSibling().select("td");
	for (Element div : divs) {
	 ratio = ratio + " " + getFirstNum(div.text());
	}
	break;
     }
   }
} catch (Exception ex) {
 	Toast.makeText(mCtx, getString(R.string.mesErrJsoup) + url, Toast.LENGTH_LONG).show();
    	Log.w("CTlist", getString(R.string.mesErrJsoup) + ex + " " + ratio);
}

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos necesarios están marcados *

*

Puedes usar las siguientes etiquetas y atributos HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>