如何在td标记jsoup java

时间:2018-04-16 20:21:43

标签: java html jsoup

我需要帮助。 我想在此网站https://www.servientrega.com/RastreoContado/RastreoContado2.faces?idGuia=2003159909&idPais=1

中获取此表格的所有td文字

Table with information what I want - image

。但是有一个特殊的td标签带有超链接。

我只想获得详细信息包表。

special td tag - image

My result and that I don't want - image

修改

我想获取id = form01:tableEx4_data的tbody行 第三张图片有我想要的东西。

部分代码网https://www.servientrega.com/RastreoContado/RastreoContado2.faces?idGuia=2003159909&idPais=1

    <tbody id="form01:tableEx4_data" class="ui-datatable-data ui-widget-content">
 <tr data-ri="0" class="ui-widget-content ui-datatable-even" role="row">
  <td role="gridcell"><span class="outputText">GUIA GENERADA</span></td>
  <td role="gridcell"><span class="outputText">BOGOTA (CUNDINAMARCA)</span></td>
  <td role="gridcell"><span class="outputText">04/04/2018 17:33:05</span></td>
 </tr>
 <tr data-ri="1" class="ui-widget-content ui-datatable-odd" role="row">
  <td role="gridcell"><span class="outputText">INGRESO AL CENTRO LOGISTICO</span></td>
  <td role="gridcell"><span class="outputText">BOGOTA (CUNDINAMARCA)</span></td>
  <td role="gridcell"><span class="outputText">04/05/2018 01:35:25</span></td>
 </tr>
 <tr data-ri="2" class="ui-widget-content ui-datatable-even" role="row">
  <td role="gridcell"><a href="#" id="form01:tableEx4:2:linkDesMov11" name="form01:tableEx4:2:linkDesMov11">SALIO A CIUDAD DESTINO</a>
   <div id="form01:tableEx4:2:tooltip_linkDesMov11" class="ui-tooltip ui-widget ui-widget-content ui-shadow ui-corner-all"> 
    <div> 
     <div style="display: none;">
      Tipo moviento: |2|
     </div>
     <table id="form01:tableEx4:2:j_id1394398698_531cdaa3" class="ui-panelgrid ui-widget dataTableEx" style="min-width: 200px; max-width: 400px;" role="grid">
      <tbody>
       <tr class="ui-widget-content" role="row">
        <td role="gridcell">
         <table id="form01:tableEx4:2:j_id1394398698_531cda89" class="ui-panelgrid ui-widget headerClass2" style="width: 100%; min-width: 200px; max-width: 400px;" role="grid">
          <tbody>
           <tr class="ui-widget-content" role="row"></tr>
           <tr class="ui-widget-content" role="row">
            <td role="gridcell"><span style="width: 100%" class="outputText">Novedad</span></td>
           </tr>
          </tbody>
         </table></td>
       </tr>
       <tr class="ui-widget-content" role="row">
        <td role="gridcell">
         <table id="form01:tableEx4:2:j_id1394398698_531cda7d" class="ui-panelgrid ui-widget headerClass2" style="width: 100%; min-width: 200px; max-width: 400px;" role="grid">
          <tbody>
           <tr class="ui-widget-content" role="row"></tr>
           <tr class="ui-widget-content" role="row">
            <td role="gridcell"><span style="width: 30%" class="outputText">Fecha Probable Entrega</span></td>
            <td role="gridcell"><span style="width: 70%" class="outputText">Descripción de la novedad</span></td>
           </tr>
          </tbody>
         </table></td>
       </tr>
       <tr class="ui-widget-content" role="row">
        <td role="gridcell">
         <table id="form01:tableEx4:2:j_id1394398698_531cda0f" class="ui-panelgrid ui-widget" style="width: 100%; min-width: 200px; max-width: 400px;" role="grid">
          <tbody>
           <tr class="ui-widget-content" role="row"></tr>
           <tr class="ui-widget-content" role="row">
            <td role="gridcell"><span style="width: 30%" class="outputText">07/04/2018</span></td>
            <td role="gridcell"><span style="width: 70%" class="outputText"></span></td>
           </tr>
          </tbody>
         </table></td>
       </tr>
      </tbody>
     </table> 
    </div>
   </div>

我可以获得td标签(表格行):GUIA GENERADA,BOGOTA(CUNDINAMARCA),04/04/201 17:33:05 - INGRESO AL CENTRO LOGISTICO,BOGOTA(CUNDINAMARCA),04/05/201 01 :35:25但是“SALIO A CIUDAD DESTINO”这一行显示了我不想要的更多细节。只有我想要文本“SALIO A CIUDAD DESTINO”。

2 个答案:

答案 0 :(得分:0)

也许这可以帮到你。

WebElement datewidget = driver
            .findElement(By.id("form01:tableEx4_data"));
    List<WebElement> rows = datewidget.findElements(By.tagName("tr"));
    List<WebElement> columns = datewidget.findElements(By.tagName("td"));

    for (WebElement cell : columns) {
        if (cell.getText().equals("SALIO A CIUDAD DESTINO")) {
            cell.click();
            break;
        }
    }

答案 1 :(得分:0)

如果您使用的是Jsoup,下面的代码可能会对您有所帮助。

 Element form = document.getElementById("form01:tableEx4_data");
 Elements tdList = form.getElementsByTag("td");
 for(Element td: tdList) {
       System.out.println(td.text()); // td.text() is different from td.html() 
  }

结果如下:

GUIA GENERADA
BOGOTA (CUNDINAMARCA)
04/04/2018 17:33:05
INGRESO AL CENTRO LOGISTICO
BOGOTA (CUNDINAMARCA)
04/05/2018 01:35:25
SALIO A CIUDAD DESTINO
BOGOTA (CUNDINAMARCA)
04/05/2018 22:43:17
INGRESO AL CENTRO LOGISTICO
BARRANQUILLA (ATLANTICO)
04/06/2018 23:57:50
EN ZONA DE DISTRIBUCION
BARRANQUILLA (ATLANTICO)
04/09/2018 06:24:10
REPORTADO ENTREGADO
BARRANQUILLA (ATLANTICO)
04/09/2018 12:48:58
ENTREGA VERIFICADA
BARRANQUILLA (ATLANTICO)
04/09/2018 17:54:44

但是,如果您正在使用其他图书馆,您还可以在&#39; a&#39;之间获取文字。使用正则表达式标记。

String s = "<a href=\"https://www.servientrega.com/RastreoContado/RastreoContado2.faces?idGuia=2003159909&amp;idPais=1#\" id=\"form01:tableEx4:2:linkDesMov11\" name=\"form01:tableEx4:2:linkDesMov11\">SALIO A CIUDAD DESTINO</a>";
final Pattern pattern = Pattern.compile(">(.+?)<");
final Matcher matcher = pattern.matcher(s);
matcher.find();
System.out.println(matcher.group(1));

在这种情况下,结果将是:

SALIO A CIUDAD DESTINO