2015-03-03 46 views
0

我是第一個cs學生,在一個項目上工作,我需要從網站獲取溫度日期。我試圖用Jsoup來做到這一點,但我發現缺乏入門教程。這裏是我試圖解析的HTML。我將如何去使用jsoup從這個HTML解析特定的整數值?

<span class="actual-temp ng-isolate-scope ng-binding" aria-label="Actual Temperature" data-wx-temperature="obs.getTemp()" data-show-temp-unit="true"> 
     <!-- ngIf: hasValue() --> 
     <span data-ng-if="hasValue()" data-ng-bind-template="32" class="ng-scope ng-binding">32</span> 
     <!-- end ngIf: hasValue() --> 
     <!-- ngIf: hasValue() --> 
     <sup data-ng-if="hasValue()" class="deg ng-scope">°</sup> 
     <!-- end ngIf: hasValue() --> 
     <!-- ngIf: showTempUnit --> 
     <sup class="temp-unit ng-scope ng-binding" data-ng-if="showTempUnit" data-ng-bind-template="F">F</sup> 

具體來說,我試圖從中提取數字32。任何幫助將不勝感激!

+2

讓我看看迄今爲止所做的工作。我們將從那裏開始。 – 2015-03-03 05:29:04

+0

這裏有一個很棒的jsoup教程:http://jsoup.org/cookbook/ – 2015-03-03 05:32:54

回答

1

根據您提供的一小段代碼來提取溫度。您可能需要修改代碼才能使用完整的HTML。 (爲簡單起見,所有檢查或優化有意省略)

Document doc = Jsoup.parse("<span class=\"actual-temp ng-isolate-scope ng-binding\" aria-label=\"Actual Temperature\" data-wx-temperature=\"obs.getTemp()\" data-show-temp-unit=\"true\">\n" 
     + "  <!-- ngIf: hasValue() -->\n" 
     + "  <span data-ng-if=\"hasValue()\" data-ng-bind-template=\"32\" class=\"ng-scope ng-binding\">32</span>\n" 
     + "  <!-- end ngIf: hasValue() -->\n" 
     + "  <!-- ngIf: hasValue() -->\n" 
     + "  <sup data-ng-if=\"hasValue()\" class=\"deg ng-scope\">°</sup>\n" 
     + "  <!-- end ngIf: hasValue() -->\n" 
     + "  <!-- ngIf: showTempUnit -->\n" 
     + "  <sup class=\"temp-unit ng-scope ng-binding\" data-ng-if=\"showTempUnit\" data-ng-bind-template=\"F\">F</sup>\n" 
     + " </span>"); 
Elements rows = doc.getElementsByAttributeValue("class", "ng-scope ng-binding"); 
String temperature; 
for (Element span : rows) { 
     temperature = span.text(); 
} 
System.out.println("temperature = " + temperature);