使用JSoup刮取Google天气结果

时间:2015-09-21 09:18:39

标签: java android html parsing jsoup

我试图从谷歌网页获取天气状况,并且HTML元素wob_tm上有一个标记,我试图获取值,但总是得到异常

 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

1 个答案:

答案 0 :(得分:1)

查询中存在错误,我应该定义用户代理

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