簡體   English   中英

我將如何使用jsoup從此HTML解析特定的整數值?

[英]How would I go about using jsoup to parse a specific integer value from this HTML?

我是一年級CS學生,正在從事一個項目,我需要從網站上獲取溫度數據。 我正在嘗試使用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。 任何幫助將不勝感激!

根據您提供的少量代碼,可以提取溫度。 您可能需要修改代碼才能使用完整的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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM