0
我需要捕獲有關天氣的一些信息。我使用JSoup,但我將xPath轉換爲Elements時遇到了問題。你知道爲什麼嗎??我有點困惑。XPath轉換爲JSoup元素
我建立這個代碼
String newUrl= new String("http://www.weather.com/wx/today/?lat="+lat+"&lon="+lon+"+&locale=en_US");
String tmpS=new String("http://www.weather.com/wx/today/?lat=52.450988049&lon=20.66802978515625&locale=en_US");
URL example = new URL(tmpS);
BufferedReader in = new BufferedReader(new InputStreamReader(example.openStream()));
StringBuilder websiteSource = new StringBuilder();
String tmp = null;
while ((tmp = in.readLine()) != null) {
websiteSource.append(tmp);
}
in.close();
System.out.println(websiteSource.toString());
Document doc = Jsoup.parse(websiteSource.toString());
////*[@id="wx-local-wrap"]/div[2]/div[2]/div/div/div/div/article/div/div/article/div/section[1]/section[1]/div[1]/div
Elements h1Elements = doc.select("#wx-local-wrap > div:eq(1) > div:eq(1) > div> div > article > div > selection > selection > div > div")
您可以嘗試使用fiddler2調查對weather.com的請求。也許你可以直接請求獲取所需的數據。 –
我的回答有幫助嗎? – alkis