简体   繁体   English

JSoup获取表的内容

[英]JSoup To Get Contents of Table

I'm trying to extract the temperature value (in the table below 51.46) and the 6.43 for the pressure from the table below using JSoup for Android. 我正在尝试使用Android的JSoup从下表中提取温度值(在下表51.46中)和压力6.43。 Please note that that 51.46 and 6.43 change as new temperature and pressure readings are taken 请注意,随着获取新的温度和压力读数,51.46和6.43会发生变化

<tr class="time"><td colspan="3">at 10-03-2014 23:15:00</td></tr>
<tr><td class="param" title="Temperature" rel="tooltip">Temperature</td><td class="value">51.46</td><td>F</td></tr>
<tr><td class="param" title="Pressure" rel="tooltip">Pressure</td><td class="value">6.43</td><td>psi</td></tr>
<tr><td class="param" title="Level" rel="tooltip">Level</td><td class="value">-1.00</td><td>ft</td></tr>
<tr><td class="param" title="Cell End" rel="tooltip">Cell End</td><td class="value">13.100</td><td>ft</td></tr>

This should work. 这应该工作。 Keep in mind that you need to have the s in between or it will be stripped off when you are parsing it as a Jsoup Document. 请记住,您需要在中间插入,否则在将其解析为Jsoup Document时将被剥离。

File file = new File("test2.html");
try{
    Document doc = Jsoup.parse(file, "UTF-8");
    Element temperatureTd = doc.select("td[title=Temperature]").first();
    Element pressureTd = doc.select("td[title=Pressure]").first();

    String temperature = temperatureTd.nextElementSibling().text() + " "
            + temperatureTd.nextElementSibling().nextElementSibling().text();
    System.out.println(String.format("Temperature: %s", temperature));
    String pressure = pressureTd.nextElementSibling().text() + " "
            + pressureTd.nextElementSibling().nextElementSibling().text();
    System.out.println(String.format("Pressure: %s", pressure));

} catch (Exception e){
    e.printStackTrace();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM