简体   繁体   English

如何使用Jsoup解析此html表

[英]How to parse this html table with Jsoup

I have this HTML table: 我有这个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>

And I am parsing it with this code: 我用下面的代码解析它:

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());
        }
    }
}

And what I am seeing in the logcat is: 我在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

but what I want is this: 但是我想要的是:

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

Actually I don't have control over the number of rows but I know the columns are always two. 实际上,我无法控制行数,但我知道列始终为两列。 And also the keys and values vary. 并且键和值也有所不同。

Please do you know how I could do it? 请您知道我该怎么做吗?

Try this you can change sysout to Log 试试这个你可以将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());
            }
        }

    }


}

Output: 输出:

   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

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

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