giozh
giozh

Reputation: 10068

Jsoup, unable to retrieve object

I'm trying to use jsoup api, and my first try is to retrieve from google.it text of search button and show it inside a textview. i've used this code:

protected String doInBackground(Void... params) {
    Document doc;
    try {
        doc = Jsoup.connect("http://www.google.it/").get();
        Elements cerca_con_google = doc.select("button[id=gbqfba[aria-label]]");
            int size = cerca_con_google.size();
            Log.i("AAAAAAAAAA", Integer.toString(size));

            if(cerca_con_google != null) {
                return cerca_con_google.text();
            }   
            return "foo";
}

but size of elements is always zero. Maybe i'm wrong with the selector query inside select method... Or something else?

Upvotes: 1

Views: 206

Answers (2)

Vitaly
Vitaly

Reputation: 2790

Google returns different content based on header values, e.g. User-Agent.

public static void main(String... args) throws IOException {
    {
        System.out.println("-- Default header");
        Connection conn = Jsoup.connect("http://www.google.it/");
        Document document = conn.get();

        {
            Elements elems = document.select("input");
            System.out.println("input - " + elems.size());
            for (Element elem : elems) {
                System.out.println("input [" + elem.attributes() + "]");
            }
        }
        {
            Elements elems = document.select("button");
            System.out.println("button - " + elems.size());
            for (Element elem : elems) {
                System.out.println("button [" + elem.attributes() + "]");
            }
        }
    }
    {
        System.out.println("-- Custom header");
        Connection conn = Jsoup.connect("http://www.google.it/");
        conn.header("User-Agent", "Firefox/20.0");
        Document document = conn.get();

        {
            Elements elems = document.select("input");
            System.out.println("input - " + elems.size());
            for (Element elem : elems) {
                System.out.println("input [" + elem.attributes() + "]");
            }
        }
        {
            Elements elems = document.select("button");
            System.out.println("button - " + elems.size());
            for (Element elem : elems) {
                System.out.println("button [" + elem.attributes() + "]");
            }
        }
        {
            Element elem = document.select("button#gbqfbb").first();
            System.out.println();
            System.out.println("button#gbqfbb = " + elem);
        }
    }
}

Output

-- Default header
input - 7
input [ name="ie" value="ISO-8859-1" type="hidden"]
input [ value="it" name="hl" type="hidden"]
input [ name="source" type="hidden" value="hp"]
input [ autocomplete="off" class="lst" value="" title="Cerca con Google" maxlength="2048" name="q" size="57" style="color:#000;margin:0;padding:5px 8px 0 6px;vertical-align:top"]
input [ class="lsb" value="Cerca con Google" name="btnG" type="submit"]
input [ class="lsb" value="Mi sento fortunato" name="btnI" type="submit" onclick="if(this.form.q.value)this.checked=1; else top.location='/doodles/'"]
input [ type="hidden" id="gbv" name="gbv" value="1"]
button - 0
-- Custom header
input - 4
input [ type="hidden" name="output" value="search"]
input [ type="hidden" name="ie" value="UTF-8"]
input [ type="hidden" name="sclient" value="psy-ab"]
input [ id="gbqfq" class="gbqfif" name="q" type="text" autocomplete="off" value=""]
button - 3
button [ id="gbqfb" aria-label="Cerca con Google" class="gbqfb" name="btnG"]
button [ id="gbqfba" aria-label="Cerca con Google" name="btnK" class="gbqfba"]
button [ id="gbqfbb" aria-label="Mi sento fortunato" name="btnI" class="gbqfba" onclick="if(this.form.q.value)this.checked=1;else window.top.location='/doodles/'"]

button#gbqfbb = <button id="gbqfbb" aria-label="Mi sento fortunato" name="btnI" class="gbqfba" onclick="if(this.form.q.value)this.checked=1;else window.top.location='/doodles/'"><span id="gbqfsb">Mi sento fortunato</span></button>

Upvotes: 2

TomTasche
TomTasche

Reputation: 5526

I think you're using the wrong selector. Use "button[id=gbqfba]" instead.

Upvotes: 0

Related Questions