我试图从谷歌网页获取天气状况,并且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);
}
答案 0 :(得分:1)
查询中存在错误,我应该定义用户代理
Document doc = Jsoup
.connect("https://www.google.com/search?q=mario");
.userAgent("Mozilla/5.0")
.timeout(5000).get();