简体   繁体   中英

Extract specific html table content using Jsoup

I want to extract sepecific content from a responsive HTML table, I am using Jsoup.

Here is the structure of my table :

<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>

I only one to extract the value between bold tags, for instance for the first TD the value is TASK_TOTO.

Here is my JAVA code :

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

}

I am a newbie to JSOUP and my code does not diplay anything so far. I am using the table id to locate the table.

Thanks For your help

FYI : My table is generated using angular JS so Jsoup is not the best way to extract the table data.

When using this code instead :

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

I still don't get the content displayed and the size is set to 1!! I am not sure what I am doing wrong!!

Well, based on the HTML snippet you provided, the table's id is main_widget_table , not search_results_table . (The URL in your code is no longer accessible, so I can't tell if there's some other search_results_table on that page.)

You can print the text of all b tags in that table with

for (Element e : doc.select("#main_widget_table b"))
    System.out.println(e.text());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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