0

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!

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

7 Comments

this keeps returning an empty value
public class Main { public static void main(String[] args) throws IOException { Document docDolarHoy = Jsoup.connect("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); } }
output: dolarHoy: $ 30.84
Hi, if a use "dolarhoy.com" it returns Malformed URL: dolarhoy.com and if a use "dolarhoy.com" it throws nullpointer
Right, when I've pasted my code as a comment SO changed it. It should be http : // www . dolarhoy.com without the spaces of course.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.