2015-09-21 64 views
0

我試圖從谷歌網頁得到天氣情況,並有一個標籤上的HTML元素wob_tm,我試圖得到的價值,但總是我得到一個異常使用JSoup刮谷歌天氣結果

try { 
     Document document = Jsoup.connect("https://www.google.jo/webhp?hl=en#hl=en-JO&q=amman+weather").get(); 
     Element temp = document.getElementById("wob_tm"); 
     System.out.println(temp.text()); // Exception in thread "main" java.lang.NullPointerException 
    } catch (IOException ex) { 
     Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); 
    } 

enter image description here

+0

你得到了什麼異常? – luksch

+0

'NullPointerException' – tamtom

+0

您是否嘗試將整個文檔打印到您的LogCat以檢查您實際獲得的內容? – freddieptf

回答

1

有查詢的錯誤,我應該定義一個用戶代理

Document doc = Jsoup 
    .connect("https://www.google.com/search?q=mario"); 
    .userAgent("Mozilla/5.0") 
    .timeout(5000).get();