【发布时间】:2018-04-10 05:03:09
【问题描述】:
我之前问过一个关于这个的问题,我离我的主要目标更近了,但仍然没有达到我想要的目标。我当前的代码:
try{
Connection formPage = Jsoup.connect("https://www.google.com/search?q=weather&oq=weather&aqs=chrome..69i57j69i61j69i60j0l3.3806j0j7&sourceid=chrome&ie=UTF-8");
formPage.timeout(1000)
.method(Connection.Method.GET)
.userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36")
.execute();
Document getformPage = formPage.get();
//Element getformPage = formPage.getElementById("wob_t");
System.out.println(getformPage.getElementById("wob_t"));
//System.out.println(formPage.request().url());
}catch(Exception exception){
System.exit(0);
}
返回一个空表达式。我希望它给出当前的天气。任何帮助,将不胜感激。谢谢!
【问题讨论】:
-
您传递的 id 是否正确,我认为应该是
getformPage.getElementById("wob_tm") -
AFAIK,Google 仅使用 JavaScript 来显示此内容。因此,JSoup 几乎没用。你最好看看 Selenium
-
@r3dst0rm 你有没有检查过谷歌提供的
weather page的页面源,OP 需要的所有细节都在其中,并且可以使用 Jsoup 轻松地废弃/解析。 Jsoup 将读取页面源中存在的所有详细信息:) -
@Rishaldevsingh 因为我只是想知道我们从 OP 的 URL 中得到了什么,所以我使用了
try.jsoup.org并在那里获取了给定的 URL。显然没有结果 HTML,因为我们会在浏览器中获取它来抓取天气信息。 -
@CalebDunham 您已将方法
getformPage.getElementsByClass("wob_t")替换为getformPage.getElementById("wob_t"))stackoverflow.com/questions/49196106/…