2015-06-03 169 views
1

這可能是個愚蠢的問題,但我無法真正理解它。我試圖從頁面解析html輸出:http://meteo.uwb.edu.pl/Jsoup,解析html表

所以基本上我需要從表格中提取值,從左側(藍色文本)作爲鍵(標題)和從右側(棕色文本)作爲值。另外,標題標籤(「Aktualna pogoda/Weather conditions:」)

我的意圖是從html輸出中獲取html表格,然後解析其行,但我無法弄清楚,因爲html輸出相當複雜。我從它開始:

doc = Jsoup.connect("http://meteo.uwb.edu.pl/").get(); 
Elements tables = doc.select("table"); 
for (Element row : table.select("tr")) 
{ 
    Elements tds = row.select("td:not([rowspan])"); 
    System.out.println(tds.get(0).text() + "->" + tds.get(1).text()); 
} 

但仍然我的結果是一團糟。你有任何想法如何正確解析它?

從第一表

回答

3

密鑰數據可通過該代碼來檢索:

doc.select("table").get(1).select("tbody").get(1).select("tr").get(1).select("td").get(0).select("b") 

和值由這樣的:對於第二表格

doc.select("table").get(1).select("tbody").get(1).select("tr").get(1).select("td").get(1).select("b") 

doc.select("table").get(2).select("tbody").get(0).select("tr").get(1).select("td").get(0).select("b") 

doc.select("table").get(2).select("tbody").get(0).select("tr").get(1).select("td").get(1).select("b") 
+0

謝謝,你的靈魂看起來比我的好,我會檢查它 – user1209216

0

我設法這樣說:

doc = Jsoup.connect("http://meteo.uwb.edu.pl/").get(); 
Elements tables = doc.select("td"); 
Elements headers = tables.get(2).select("b"); 
Elements vals = tables.get(3).select("b"); 
Map all = new HashMap(); 

for (int i=0;i<headers.size() ; i++) all.put(headers.get(i).text(),vals.get(i).text()); 

這似乎是確定。