gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 15:23:44 25 4
gpt4 key购买 nike

我想从响应式 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!!我不知道我做错了什么!!

最佳答案

嗯,根据您提供的 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());

关于java - 使用Jsoup提取特定的html表格内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20095263/

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