如何用Jsoup解析这个html表

时间:2016-07-07 13:05:10

标签: java jsoup

我有这个HTML表格:

<table class="prk-fields">
    <tbody>
        <tr class="field_1 visibility-public field_type_textbox">
            <td class="label">Date</td>
            <td class="data">
                <p>"2144"</p>
            </td>
        </tr>
        <tr class="field_3 visibility-public alt field_type_textbox">
            <td class="label">Location</td>
            <td class="data">
                <p>Planet Earth</p>
            </td>
        </tr>
        <tr class="field_4 visibility-public field_type_url">
            <td class="label">By</td>
            <td class="data">
                <p><a href="https://en.wikipedia.org/wiki/Extraterrestrial_life">Extraterrestrials</a>
                </p>
            </td>
        </tr>
        <tr class="field_5 visibility-public alt field_type_url">
            <td class="label">Victims</td>
            <td class="data">
                <p>0</p>
            </td>
        </tr>
        <tr class="field_6  visibility-public field_type_textbox">
            <td class="label">Reaction</td>
            <td class="data">
                <p>Apathetic</p>
            </td>
        </tr>
        <tr class="field_7 visibility-public alt field_type_textarea">
            <td class="label">About</td>
            <td class="data">
                <p>it's about the 2144 attack on Earth by extraterrestrials</p>
            </td>
        </tr>
    </tbody>
</table>

我用这段代码解析它:

Document document = Jsoup.parse(response);
int index  = 0;
for (Element td : document.select("td")) {
    Log.d(TAG, "Row" + (++index));
    for (Attribute attr : td.attributes()) {
        Log.d(TAG, "TD " + attr.getKey() + " : " + attr.getValue());
    }
    for (Element p : td.select("p")) {
        for (Attribute attr : td.attributes()) {
            Log.d(TAG, "TTD " + attr.getKey() + " :: " + attr.getValue());
        }
    }
}

我在logcat中看到的是:

Row1
TD class : label
Row2
TD class : data
TTD class :: data
Row3
TD class : label
Row4
TD class : data
TTD class :: data
Row5
TD class : label
Row6
TD class : data
TTD class :: data

但我想要的是:

Row1
TD Date : "2144"
Row2
TD Location : Planet Earth
Row3
TD By : Extraterrestrials
Row4
TD Victims : 0
Row5
TD Reaction : Apathetic
Row6
TD About : it's about the 2144 attack on Earth by extraterrestrials

实际上我无法控制行数,但我知道列总是两行。而且键和值也各不相同。

请你知道我怎么做吗?

1 个答案:

答案 0 :(得分:1)

试试这个,您可以将sysout更改为Log

   public class Test {
    public static void main(String[] args) {
        String response="<table class=\"prk-fields\">\n" +
                "    <tbody>\n" +
                "        <tr class=\"field_1 visibility-public field_type_textbox\">\n" +
                "            <td class=\"label\">Date</td>\n" +
                "            <td class=\"data\">\n" +
                "                <p>\"2144\"</p>\n" +
                "            </td>\n" +
                "        </tr>\n" +
                "        <tr class=\"field_3 visibility-public alt field_type_textbox\">\n" +
                "            <td class=\"label\">Location</td>\n" +
                "            <td class=\"data\">\n" +
                "                <p>Planet Earth</p>\n" +
                "            </td>\n" +
                "        </tr>\n" +
                "        <tr class=\"field_4 visibility-public field_type_url\">\n" +
                "            <td class=\"label\">By</td>\n" +
                "            <td class=\"data\">\n" +
                "                <p><a href=\"https://en.wikipedia.org/wiki/Extraterrestrial_life\">Extraterrestrials</a>\n" +
                "                </p>\n" +
                "            </td>\n" +
                "        </tr>\n" +
                "        <tr class=\"field_5 visibility-public alt field_type_url\">\n" +
                "            <td class=\"label\">Victims</td>\n" +
                "            <td class=\"data\">\n" +
                "                <p>0</p>\n" +
                "            </td>\n" +
                "        </tr>\n" +
                "        <tr class=\"field_6  visibility-public field_type_textbox\">\n" +
                "            <td class=\"label\">Reaction</td>\n" +
                "            <td class=\"data\">\n" +
                "                <p>Apathetic</p>\n" +
                "            </td>\n" +
                "        </tr>\n" +
                "        <tr class=\"field_7 visibility-public alt field_type_textarea\">\n" +
                "            <td class=\"label\">About</td>\n" +
                "            <td class=\"data\">\n" +
                "                <p>it's about the 2144 attack on Earth by extraterrestrials</p>\n" +
                "            </td>\n" +
                "        </tr>\n" +
                "    </tbody>\n" +
                "</table>";

        Document document = Jsoup.parse(response);
               int index=0;
        for (Element table : document.select("table")) {

            for (Element row : table.select("tr")) {
                System.out.println("Row\t" + (++index));
                Elements tds = row.select("td");
                System.out.println("TD\t" +tds.get(0).text()+":"+tds.get(1).text());
            }
        }

    }


}

输出:

   Row  1
TD  Date:"2144"
Row 2
TD  Location:Planet Earth
Row 3
TD  By:Extraterrestrials
Row 4
TD  Victims:0
Row 5
TD  Reaction:Apathetic
Row 6
TD  About:it's about the 2144 attack on Earth by extraterrestrials