2016-12-05 62 views
0

良好的一天,使用JSoup提取特定表(TableHeaderValue - > TableBodyValue)目錄

我使用jsoup從表中提取數據

內容表是

<table class="compare-products-table compare-products"> 
    <thead> 
     <tr> 
      <th> 
      <p>GPSMAP</p> 
      </th> 
      <th>7x1</th> 
      <th>8x0/10x0</th> 
      <th>4000/5000</th> 
      <th>6000/7000</th> 
      <th>7400/7600</th> 
      <th>8000/8500</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td>Radar Overlay</td> 
      <td class="checked">•</td> 
      <td class="checked">•</td> 
      <td class="checked">•</td> 
      <td class="checked">•</td> 
      <td class="checked">•</td> 
      <td class="checked">•</td> 
     </tr> 
     <tr> 
      <td>Dual Range</td> 
      <td></td> 
      <td></td> 
      <td class="checked">•</td> 
      <td class="checked">•</td> 
      <td class="checked">•</td> 
      <td class="checked">•</td> 
     </tr> 
     <tr> 
      <td>MARPA</td> 
      <td></td> 
      <td></td> 
      <td class="checked">•</td> 
      <td class="checked">•</td> 
      <td class="checked">•</td> 
      <td class="checked">•</td> 
     </tr> 
     <tr> 
      <td>True Color</td> 
      <td class="checked">•</td> 
      <td class="checked">•</td> 
      <td></td> 
      <td class="checked">•</td> 
      <td class="checked">•</td> 
      <td class="checked">•</td> 
     </tr> 
     <tr> 
      <td>Auto Bird Gain</td> 
      <td class="checked">•</td> 
      <td class="checked">•</td> 
      <td></td> 
      <td></td> 
      <td class="checked">•</td> 
      <td class="checked">•</td> 
     </tr> 
     <tr> 
      <td>Echo Trails</td> 
      <td class="checked">•</td> 
      <td class="checked">•</td> 
      <td></td> 
      <td></td> 
      <td class="checked">•</td> 
      <td class="checked">•</td> 
     </tr> 
     <tr> 
      <td>Pulse Expansion <span class="kicker pri sm">NEW</span></td> 
      <td></td> 
      <td></td> 
      <td></td> 
      <td></td> 
      <td class="checked">•</td> 
      <td class="checked">•</td> 
     </tr> 
     <tr> 
      <td>Dual Radar Support</td> 
      <td></td> 
      <td></td> 
      <td></td> 
      <td></td> 
      <td class="checked">•</td> 
      <td class="checked">•</td> 
     </tr> 
     <tr> 
      <td>Programmable antenna parking</td> 
      <td></td> 
      <td></td> 
      <td></td> 
      <td></td> 
      <td class="checked">•</td> 
      <td class="checked">•</td> 
     </tr> 
    </tbody> 
</table> 

輸出值應該是像

7x1 -> Radar Overlay: yes 
8x0/10x0 -> Radar Overlay: yes 
4000/5000 -> Radar Overlay: yes 
6000/7000 -> Radar Overlay: yes 
7400/7600 -> Radar Overlay: yes 
8000/8500 -> Radar Overlay: yes 

7x1 -> Dual Range: no 
8x0/10x0 -> Dual Range: no 
4000/5000 -> Dual Range: yes 
6000/7000 -> Dual Range: yes 
7400/7600 -> Dual Range: yes 
8000/8500 -> Dual Range: yes 

etc

例子我見過不在如何得到的內容,如果有表

我所得到的ATM的屬性昭然若揭:如果有人爲我提供

 Elements elementsFeatures = docProductsAttr.select("#featureTab"); // Feature 
     if (!elementsFeatures.isEmpty()) { 
      Elements selectThead = elementsFeatures.select(".compare-products thead tr th:gt(0)"); // get Table Head skipping 1st element 
      List<String> collectTableHead = selectThead.stream().map(i -> i.text()).collect(toList()); // collect head text value to List 
      Elements selectTbodyTr = elementsFeatures.select(".compare-products tbody tr"); // select Body tr to mix it with Head value 
     } 

可以理解的實現這一目標所需的代碼。

回答

0

試試這個:

Elements elementsFeatures = docProductsAttr.select("#featureTab"); // Feature 
if (!elementsFeatures.isEmpty()) { 
    for (Element row : elementsFeatures.select(".compare-products tbody tr")) { 
     Elements rowCells = row.select("td"); 
     String gpsMap = rowCells.first().text(); 
     int i = 1; 

     for (Element columnHeader : elementsFeatures.select(".compare-products thead tr th:gt(0)")) { 
      System.out.format("%s -> %s: %s%n", columnHeader.text(), gpsMap, yesOrNo(rowCells.get(i))); 
      i++; 
     } 
     System.out.println(); 
    } 
} 

private static String yesOrNo(Element rowCell) { 
    String ret = "no"; 
    if (rowCell.hasClass("checked")) { 
     ret = "yes"; 
    } 
    return ret; 
} 

輸出

7x1 -> Radar Overlay: yes 
8x0/10x0 -> Radar Overlay: yes 
4000/5000 -> Radar Overlay: yes 
6000/7000 -> Radar Overlay: yes 
7400/7600 -> Radar Overlay: yes 
8000/8500 -> Radar Overlay: yes 

7x1 -> Dual Range: no 
8x0/10x0 -> Dual Range: no 
4000/5000 -> Dual Range: yes 
6000/7000 -> Dual Range: yes 
7400/7600 -> Dual Range: yes 
8000/8500 -> Dual Range: yes 
... 

詳細

在CSS查詢.compare-products thead tr th:gt(0):gt(0)手段「選擇所有th除了第一個「。

+0

工作就像一個魅力! thx你 – kvizer