2013-04-28 89 views
1

我想使用jsoup API,我的第一個嘗試是從google.it搜索按鈕的文本中檢索並顯示它在一個textview內。 我已使用此代碼:Jsoup,無法檢索對象

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"; 
} 

但元素的大小始終爲零。也許我錯了選擇器查詢內選擇方法...或者別的什麼?

+0

我敢肯定,谷歌改變了元素的ID不時。這可能是問題所在。 – TomTasche 2013-04-28 11:20:31

+0

我現在匹配id元素,它們幾個小時前是一樣的。 – giozh 2013-04-28 12:55:58

回答

2

Google根據報頭值返回不同的內容,例如, 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); 
     } 
    } 
} 

輸出

-- 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> 
+0

@giozh請幫忙嗎? – Vitaly 2013-04-30 11:28:27

0

我想你使用了錯誤的選擇器。改爲使用「按鈕[id = gbqfba]」。