mmayer
mmayer

Reputation: 37

parsing html with css selector jsoup

I'm trying to get the value shown in the image below from www.dolarhoy.com Dolar Hoy

I use jsoup to do thinks like this, but the following code is not working.

private static String obtenerCotizacion() throws IOException {
    Document docDolarHoy = Jsoup.connect("http://www.dolarhoy.com").get();

    String dolar= docDolarHoy.select("div.col-md-6.venta > h4 > span").first().text();
    System.out.println("dolarHoy: " + dolar); 

    return dolar;
}

}

also probe
String dolar= docDolarHoy.select("body > div.container.body-content > div > div > div.col-md-8 > div.row > div.col-md-6.venta > h4 > span").first().text();

and

String dolar= docDolarHoy.select("div.col-md-6:nth-child(2) > h4:nth-child(1) > span:nth-child(1)").first().text();

this gives me back a empty value.

Any suggestion?

Thanks!

Upvotes: 0

Views: 70

Answers (1)

TDG
TDG

Reputation: 6151

Using my browser's developer tools I got the following selector - div.col-md-6:nth-child(2) > h4:nth-child(1) > span:nth-child(1).
If you still don't get it - add the userAgent string of your browser to the get request, something like -

Document docDolarHoy = Jsoup.connect("http://www.dolarhoy.com")
    .userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0")
    .get();

EDIT After some trials the full working code (minus exception handling) is:

Public static void main(String[] args) throws IOException {

    Document docDolarHoy = Jsoup.connect("http://www.dolarhoy.com")
            .userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0")
            .get();
    String dolar= docDolarHoy.select("div.col-md-6.venta > h4 > span").first().text();
    System.out.println("dolarHoy: " + dolar);
    System.out.println(docDolarHoy.html());     
}

And the output:

dolarHoy: $ 30.84

Upvotes: 3

Related Questions