1

我想要结果为苹果:1,橙:2,梨:3。点 (...) 表示其数量和名称未知但在 3 列中相似的其他标签。有人可以帮忙吗?谢谢。

    <tr>
    <td> 
      <span>
         .....
          <h>apple</h>
         .....
      </span>  
     </td>
     <td> 
       <span>
             .....
              <h>orange</h>
             .....
          </span>
        </td>
        <td> 
          <span>
             .....
              <h>pear</h>
             .....
          </span>
        </td>
   </tr>
4

1 回答 1

0

您可以调用getElementsByTag()任何元素以获取给定类型的所有后代元素(element.getElementsByTag("h")将获取全部<h>),然后您可以使用简单的 for 遍历结果以获取顺序。

请参阅示例代码:

import org.jsoup.Jsoup;
import org.jsoup.nodes.*;
import org.jsoup.select.*;
public class JsoupHtmlSiblingsOrder {
    public static void main(String[] args) {
        String html = "<html><body><span>HELLO!</span><table id=\"myTable\"><tbody>        " +
                "<tr><td> <span>                                                           " +
                "         .....                                                            " +
                "         <h>apple</h>                                                     " +
                "         .....                                                            " +
                "         </span>                                                          " +
                "</td><td><span>                                                           " +
                "         .....                                                            " +
                "         <h>orange</h>                                                    " +
                "         .....                                                            " +
                "         </span>                                                          " +
                "</td><td><span>                                                           " +
                "         .....                                                            " +
                "         <h>pear</h>                                                      " +
                "         .....                                                            " +
                "         </span>                                                          " +
                "</td></tr>                                                                " +
                "</tbody></table></body></html>                                            ";
        Document doc = Jsoup.parse(html);
        Element table = doc.getElementById("myTable");;
        Elements hs = table.getElementsByTag("h");
        for (int i = 0; i < hs.size(); i++) {
            Element h = hs.get(i);
            System.out.println(h.text()+":"+(i+1));
        }
    }
}

输出:

apple:1
orange:2
pear:3
于 2013-06-30T18:26:34.170 回答