gpt4 book ai didi

java - 如何用 Jsoup 解析这个 html 表

转载 作者:行者123 更新时间:2023-12-01 09:43:39 24 4
gpt4 key购买 nike

我有这个 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

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

请问你知道我该怎么做吗?

最佳答案

尝试一下,您可以将 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

关于java - 如何用 Jsoup 解析这个 html 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38246281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com