简体   繁体   English

我将如何使用jsoup从此HTML解析特定的整数值?

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

I am a first year cs student working on a project for which I need to get the temperature date from a website. 我是一年级CS学生,正在从事一个项目,我需要从网站上获取温度数据。 I am attempting to do this with Jsoup, but I am finding a lack of good tutorials on getting started. 我正在尝试使用Jsoup进行此操作,但是我发现缺少很好的入门指南。 Here is the HTML I am trying to parse from. 这是我试图从中解析的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>

Specifically, I am trying to extract the number 32 from that. 具体来说,我正在尝试从中提取数字32。 Any help would be greatly appreciated! 任何帮助将不胜感激!

Based on your little piece of code you provided following would extract the temperature. 根据您提供的少量代码,可以提取温度。 You probably need to amend the code to work with the full HTML. 您可能需要修改代码才能使用完整的HTML。 (for simplicity all checks or optimizations intentionally omitted) (为简单起见,有意省略所有检查或优化)

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