使用Jsoup提取特定的html表内容

时间:2013-11-20 11:54:51

标签: java html jsoup

我想从响应式HTML表中提取特定内容,我正在使用Jsoup。

这是我桌子的结构:

<table id="main_widget_table" class="table table-striped table-hover table-condensed table-bordered">
                <tbody>
                <!-- ngRepeat: object in currentView --><tr ng-repeat="object in currentView" class="ng-scope">
                    <td>
                        <a id="main_widget_table_object_name_action" href="#//object/" target="_blank">
                            <b class="ng-binding">TASK_BACKUP</b>
                        </a>

                        <p style="font-size:11px">
                            <span class="text-success" ng-show="object.label"><em class="ng-binding"> task backup</em></span>
                            <br ng-show="object.label">
                            <span ng-show="object.session" class="ng-binding" style="display: none;">
                                <span class="label label-default">WORKFLOW</span> &nbsp; <em class="ng-binding">
                                </em>
                            </span>
                            <br ng-show="object.session" style="display: none;">
                            <span ng-hide="object.session" class="ng-binding">
                                <span class="label label-default">JOB</span> &nbsp; <em class="ng-binding"></em>
                            </span>
                            <br ng-hide="object.session">
                            <span class="text-warning ng-binding">Location: TASKMUBACKUP</span>
                            <span ng-show="isShowing('nextPlanified')" class="badge pull-right ng-binding" style="display: none;">

                            </span>
                        </p>
                    </td>
                </tr><tr ng-repeat="object in currentView" class="ng-scope">
                    <td>
                        <a id="main_widget_table_object_name_action" href="#//object/" target="_blank">
                            <b class="ng-binding">TASK_TOTO</b>
                        </a>

                        <p style="font-size:11px">
                            <span class="text-success" ng-show="object.label"><em class="ng-binding"> task toto</em></span>
                            <br ng-show="object.label">
                            <span ng-show="object.session" class="ng-binding" style="display: none;">
                                <span class="label label-default">WORKFLOW</span> &nbsp; <em class="ng-binding">
                                </em>
                            </span>
                            <br ng-show="object.session" style="display: none;">
                            <span ng-hide="object.session" class="ng-binding">
                                <span class="label label-default">JOB</span> &nbsp; <em class="ng-binding"></em>
                            </span>
                            <br ng-hide="object.session">
                            <span class="text-warning ng-binding">Location: TASKMUTOTO</span>
                            <span ng-show="isShowing('nextPlanified')" class="badge pull-right ng-binding" style="display: none;">

                            </span>
                        </p>
                    </td>
                </tr><tr ng-repeat="object in currentView" class="ng-scope">
                    <td>
                        <a id="main_widget_table_object_name_action" href="#//object/" target="_blank">
                            <b class="ng-binding">TASK_FTP</b>
                        </a>

                        <p style="font-size:11px">
                            <span class="text-success" ng-show="object.label"><em class="ng-binding"> task ftp</em></span>
                            <br ng-show="object.label">
                            <span ng-show="object.session" class="ng-binding" style="display: none;">
                                <span class="label label-default">WORKFLOW</span> &nbsp; <em class="ng-binding">
                                </em>
                            </span>
                            <br ng-show="object.session" style="display: none;">
                            <span ng-hide="object.session" class="ng-binding">
                                <span class="label label-default">JOB</span> &nbsp; <em class="ng-binding"></em>
                            </span>
                            <br ng-hide="object.session">
                            <span class="text-warning ng-binding">Location: TASKMUFTP</span>
                            <span ng-show="isShowing('nextPlanified')" class="badge pull-right ng-binding" style="display: none;">

                            </span>
                        </p>
                    </td>
                </tr><tr ng-repeat="object in currentView" class="ng-scope">
                    <td>
                        <a id="main_widget_table_object_name_action" href="#//object/" target="_blank">
                            <b class="ng-binding">TASK_MSSQL</b>
                        </a>

                        <p style="font-size:11px">
                            <span class="text-success" ng-show="object.label"><em class="ng-binding"> task mssql</em></span>
                            <br ng-show="object.label">
                            <span ng-show="object.session" class="ng-binding" style="display: none;">
                                <span class="label label-default">WORKFLOW</span> &nbsp; <em class="ng-binding">
                                </em>
                            </span>
                            <br ng-show="object.session" style="display: none;">
                            <span ng-hide="object.session" class="ng-binding">
                                <span class="label label-default">JOB</span> &nbsp; <em class="ng-binding"></em>
                            </span>
                            <br ng-hide="object.session">
                            <span class="text-warning ng-binding">Location: TASKMUMSSQL</span>
                            <span ng-show="isShowing('nextPlanified')" class="badge pull-right ng-binding" style="display: none;">

                            </span>
                        </p>
                    </td>
                </tr><tr ng-repeat="object in currentView" class="ng-scope">
                    <td>
                        <a id="main_widget_table_object_name_action" href="#//object/" target="_blank">
                            <b class="ng-binding">TASK_ORACLE</b>
                        </a>

                        <p style="font-size:11px">
                            <span class="text-success" ng-show="object.label"><em class="ng-binding"> task oracle</em></span>
                            <br ng-show="object.label">
                            <span ng-show="object.session" class="ng-binding" style="display: none;">
                                <span class="label label-default">WORKFLOW</span> &nbsp; <em class="ng-binding">
                                </em>
                            </span>
                            <br ng-show="object.session" style="display: none;">
                            <span ng-hide="object.session" class="ng-binding">
                                <span class="label label-default">JOB</span> &nbsp; <em class="ng-binding"></em>
                            </span>
                            <br ng-hide="object.session">
                            <span class="text-warning ng-binding">Location: TASKMUORA1</span>
                            <span ng-show="isShowing('nextPlanified')" class="badge pull-right ng-binding" style="display: none;">

                            </span>
                        </p>
                    </td>
                </tr><tr ng-repeat="object in currentView" class="ng-scope">
                    <td>
                        <a id="main_widget_table_object_name_action" href="#//object/" target="_blank">
                            <b class="ng-binding">TASK_TUTU</b>
                        </a>

                        <p style="font-size:11px">
                            <span class="text-success" ng-show="object.label"><em class="ng-binding"> task tutu</em></span>
                            <br ng-show="object.label">
                            <span ng-show="object.session" class="ng-binding" style="display: none;">
                                <span class="label label-default">WORKFLOW</span> &nbsp; <em class="ng-binding">
                                </em>
                            </span>
                            <br ng-show="object.session" style="display: none;">
                            <span ng-hide="object.session" class="ng-binding">
                                <span class="label label-default">JOB</span> &nbsp; <em class="ng-binding"></em>
                            </span>
                            <br ng-hide="object.session">
                            <span class="text-warning ng-binding">Location: TASKMUTUTU</span>
                            <span ng-show="isShowing('nextPlanified')" class="badge pull-right ng-binding" style="display: none;">

                            </span>
                        </p>
                    </td>
                </tr><tr ng-repeat="object in currentView" class="ng-scope">
                    <td>
                        <a id="main_widget_table_object_name_action" href="#//object/" target="_blank">
                            <b class="ng-binding">TASK_TITI</b>
                        </a>

                        <p style="font-size:11px">
                            <span class="text-success" ng-show="object.label"><em class="ng-binding"> task titi</em></span>
                            <br ng-show="object.label">
                            <span ng-show="object.session" class="ng-binding" style="display: none;">
                                <span class="label label-default">WORKFLOW</span> &nbsp; <em class="ng-binding">
                                </em>
                            </span>
                            <br ng-show="object.session" style="display: none;">
                            <span ng-hide="object.session" class="ng-binding">
                                <span class="label label-default">JOB</span> &nbsp; <em class="ng-binding"></em>
                            </span>
                            <br ng-hide="object.session">
                            <span class="text-warning ng-binding">Location: TASKMUTITI</span>
                            <span ng-show="isShowing('nextPlanified')" class="badge pull-right ng-binding" style="display: none;">

                            </span>
                        </p>
                    </td>
                </tr><tr ng-repeat="object in currentView" class="ng-scope">
                    <td>
                        <a id="main_widget_table_object_name_action" href="#//object/" target="_blank">
                            <b class="ng-binding">TASK_WSB</b>
                        </a>

                        <p style="font-size:11px">
                            <span class="text-success" ng-show="object.label"><em class="ng-binding"> task wsb</em></span>
                            <br ng-show="object.label">
                            <span ng-show="object.session" class="ng-binding" style="display: none;">
                                <span class="label label-default">WORKFLOW</span> &nbsp; <em class="ng-binding">
                                </em>
                            </span>
                            <br ng-show="object.session" style="display: none;">
                            <span ng-hide="object.session" class="ng-binding">
                                <span class="label label-default">JOB</span> &nbsp; <em class="ng-binding"></em>
                            </span>
                            <br ng-hide="object.session">
                            <span class="text-warning ng-binding">Location: MUWSB</span>
                            <span ng-show="isShowing('nextPlanified')" class="badge pull-right ng-binding" style="display: none;">

                            </span>
                        </p>
                    </td>
                </tr><tr ng-repeat="object in currentView" class="ng-scope">
                    <td>
                        <a id="main_widget_table_object_name_action" href="#//object/" target="_blank">
                            <b class="ng-binding">TASK_SAP</b>
                        </a>

                        <p style="font-size:11px">
                            <span class="text-success" ng-show="object.label"><em class="ng-binding"> task sap</em></span>
                            <br ng-show="object.label">
                            <span ng-show="object.session" class="ng-binding" style="display: none;">
                                <span class="label label-default">WORKFLOW</span> &nbsp; <em class="ng-binding">
                                </em>
                            </span>
                            <br ng-show="object.session" style="display: none;">
                            <span ng-hide="object.session" class="ng-binding">
                                <span class="label label-default">JOB</span> &nbsp; <em class="ng-binding"></em>
                            </span>
                            <br ng-hide="object.session">
                            <span class="text-warning ng-binding">Location: FRQPMDEV18</span>
                            <span ng-show="isShowing('nextPlanified')" class="badge pull-right ng-binding" style="display: none;">

                            </span>
                        </p>
                    </td>
                </tr><tr ng-repeat="object in currentView" class="ng-scope">
                    <td>
                        <a id="main_widget_table_object_name_action" href="#//object/" target="_blank">
                            <b class="ng-binding">TASK_BATCH</b>
                        </a>

                        <p style="font-size:11px">
                            <span class="text-success" ng-show="object.label"><em class="ng-binding"> task batch</em></span>
                            <br ng-show="object.label">
                            <span ng-show="object.session" class="ng-binding" style="display: none;">
                                <span class="label label-default">WORKFLOW</span> &nbsp; <em class="ng-binding">
                                </em>
                            </span>
                            <br ng-show="object.session" style="display: none;">
                            <span ng-hide="object.session" class="ng-binding">
                                <span class="label label-default">JOB</span> &nbsp; <em class="ng-binding"></em>
                            </span>
                            <br ng-hide="object.session">
                            <span class="text-warning ng-binding">Location: MUFRQPMDE</span>
                            <span ng-show="isShowing('nextPlanified')" class="badge pull-right ng-binding" style="display: none;">

                            </span>
                        </p>
                    </td>
                </tr>
                </tbody>
            </table>

我只有一个用于提取粗体标记之间的值,例如对于第一个TD,值为TASK_TOTO。

这是我的JAVA代码:

ublic class HtmlParser {

public class HtmlParser {

public static void main(String[] args) throws Exception {
    Document doc =    Jsoup.connect("http://frstmwarwebsrv2.orsyptst.com:9000/ui/#/en/search?searchString=TSK&filterchecks=nameSWF").get();
    for (Element table : doc.select("#search_results_table")) {
        for (Element row : table.select("tr")) {
            Elements tds = row.select("td");
            System.out.println(tds.get(0).text());   
        }
    }
}

}

我是JSOUP的新手,我的代码到目前为止还没有任何内容。我正在使用表id来定位表。

由于 为了您的帮助

仅供参考:我的表是使用angular JS生成的,因此Jsoup不是提取表数据的最佳方式。

使用此代码时:

List<WebElement> resultsDiv =    driver.findElements(By.xpath("id('search_results_table')"));
         for (int i=0; i<resultsDiv.size(); i++) {
         System.out.println( resultsDiv.get(i).getText());
         System.out.println (resultsDiv.size());

我仍然没有显示内容并且大小设置为1 !!我不确定我做错了什么!!

1 个答案:

答案 0 :(得分:0)

根据您提供的HTML代码段,表格的ID为main_widget_table,而不是search_results_table。 (您的代码中的网址已无法访问,因此我无法判断该网页上是否有其他search_results_table。)

您可以使用

打印该表中所有b个标签的文字
for (Element e : doc.select("#main_widget_table b"))
    System.out.println(e.text());