0

我已经为此工作了一段时间,但似乎无法弄清楚如何获得与其拥有的标题相对应的正确表格。这些表被分成我可以检索的部分,但是在该部分内部是带有表标题的标题。我需要找到与字符串匹配的标题部分,然后从中提取数据。我可以将数据从表中取出,它只是为表获取正确的部分

该部分的 HTML 摘录:

<section class="blueTab">
            <header><h2>Energy</h2></header> //<----- THE HEADER I NEED TO MATCH TO
            <table class="infoTable">
                <tr><th>Model</th><th>0-60 mph</th><th>Top Speed</th><th>BHP</th><th></th></tr>

                        <tr>
                            <td><p>1.4i 16V Energy 5d</p></td>
                            <td><p>12.8 secs</p></td>
                            <td><p>111 mph</p></td>
                            <td><p>88 bhp</p></td>
                        </tr>

                        <tr class="alternate">
                            <td><p>1.6i 16V Energy 5d</p></td>
                            <td><p>11.5 secs</p></td>
                            <td><p>115 mph</p></td>
                            <td><p>103 bhp</p></td>
                        </tr>

                        <tr>
                            <td><p>1.8i VVT Energy 5d Auto</p></td>
                            <td><p>10.7 secs</p></td>
                            <td><p>117 mph</p></td>
                            <td><p>138 bhp</p></td>
                        </tr>

                        <tr class="alternate">
                            <td><p>1.3 CDTi 16V Energy 5d</p></td>
                            <td><p>12.8 secs</p></td>
                            <td><p>107 mph</p></td>
                            <td><p>88 bhp</p></td>
                        </tr>

            </table>

            <div class="fr topMargin">
                <div id="ctl00_contentHolder_topFullWidthContent" class="modelEnquiry">


<div id="ctl00_contentHolder_topFullWidthContent" class="buttonLinks">

</div>
<div class="cb"><!----></div>

</div>
            </div>
            <div class="cb"><!----></div>
    </section>

我猜我将不得不在 for 循环中使用 doc.getElementsByClass("blueTab") 并为每个元素查看 h2 是否等于我正在寻找的字符串,我只是不确定如何实现这个

4

1 回答 1

1

这应该可以解决您的问题

Document doc = Jsoup.parse(input, "UTF-8");
    Elements elem = doc.select(".blueTab header h2");
    for (Iterator<Element> iterator = elem.iterator(); iterator.hasNext();)
    {
        Element element = iterator.next();
        if (element.text().equals("Energy")) // your comparison  text
        {
            Element tableElement = element.parent().nextElementSibling(); //Your got the expected table Element as per your requirement
        }

    }
于 2013-06-15T23:55:27.710 回答