2016-06-01 6 views
0

私は、特定のWebサイトのテーブルを読み込み、必要なデータを取得できるプログラムを作成しようとしています。 私はjsoupと要素について読み、私が読んだものを実装しようとしましたが、何かが私のために不足している 私のテーブルには、HTMLコードがHTMLテーブルから情報を取得する必要があります。

<tr> 
 
<td valign="top" width="980px;"> 
 
<!-- START WARRANTY RESULTS \t --> 
 

 
<!-- START warrantyResultsDetails --> 
 
<table class="ibm-data-table" summary="Warranty results" border="0" cellpadding="0" cellspacing="0"> 
 
<caption><b>Warranty information</b></caption> 
 
<thead> 
 
<tr> 
 
<th scope="col">Type</th> 
 
<th scope="col">Model</th> 
 
<th scope="col">Serial number</th> 
 
</tr> 
 
</thead> 
 
<tbody> 
 
<tr> 
 
<td>8205</td> 
 
<td>E6C</td> 
 
<td>06202ET</td> 
 
</tr> 
 
</tbody> 
 
<thead> 
 
<tr> 
 
<th scope="col">Warranty status</th> 
 
<th scope="col">Expiration date</th> 
 
<th scope="col">Location</th> 
 
</tr> 
 
</thead> 
 
<tbody> 
 
<tr> 
 
<td> 
 

 

 
Out of warranty <img src="//1.www.s81c.com/i/v17/icons/_icons/ibm_icon_blue_close.png" alt="" align="middle" height="16" width="16"> 
 

 
</td> 
 
<td>2015-12-26</td> 
 
<td>ISRAEL</td> 
 
</tr> 
 
<tr> 
 
<td colspan="3"> 
 
<b>Warranty description</b> 
 
<br> 
 
This product has a 3 year limited warranty and is entitled to CRU (customer replaceable unit) and On-site labor repair service for selected parts. On-site Service is available Monday - Friday, except holidays, with a next business day response objective. A service technician will be scheduled to arrive at the customer's location on the business day after remote problem determination. 
 
</td> 
 
</tr> 
 
</tbody> 
 

 
<thead> 
 
<tr> 
 
<th scope="col" colspan="3">Additional agreement</th> 
 
</tr> 
 
</thead> 
 
<tbody> 
 
<tr> 
 
<td colspan="3"> 
 
<b> 
 
This web site provides standard warranty or eServicePac information, please consult your local IBM representative or your reseller for other maintenance services or warranty information specific to your IBM Machine. 
 
</b> 
 
</td> 
 
</tr> 
 
</tbody> 
 

 
</table> 
 
<!-- END warrantyResultsDetails --> 
 

 
<!-- END PARTS --> 
 
</td> 
 
</tr>

である私が書かれたコードを使用してみましたここstackoverではなく、それが正しい

import org.jsoup.Jsoup; 
 
import org.jsoup.nodes.Document; 
 
import org.jsoup.nodes.Element; 
 
import org.jsoup.select.Elements; 
 

 
public class Test { 
 

 
    public static void main(String[] args) throws Exception { 
 
     String url = "https://www-947.ibm.com/support/entry/portal/wlup?type=8205&serial=06202ET"; 
 
     Document document = Jsoup.connect(url).get(); 
 

 
     String question = document.select("#ibm-data-table").text(); 
 
     System.out.println("Question: " + question); 
 

 
     Elements answerers = document.select("#answers .user-details a"); 
 
     for (Element answerer : answerers) { 
 
      System.out.println("Answerer: " + answerer.text()); 
 
     } 
 
    } 
 
}

を変更することができませんでした

そして、ここでは私にすべてのデータを与える別のコードであるが、それでも、私は本当に彼ら

import java.io.IOException; 
 
import org.jsoup.Jsoup; 
 
import org.jsoup.nodes.Document; 
 
import org.jsoup.nodes.Element; 
 
import org.jsoup.select.Elements; 
 

 
public class TableEg { 
 
    public static void main(String[] args) { 
 
     String html = "https://www-947.ibm.com/support/entry/portal/wlup?type=8205&serial=06202ET"; 
 
      
 
     try { 
 
     Document doc = Jsoup.connect(html).get(); 
 
     Elements tableElements = doc.select("table"); 
 

 
     Elements tableHeaderEles = tableElements.select("thead tr th"); 
 
     System.out.println("headers"); 
 
     for (int i = 0; i < tableHeaderEles.size(); i++) { 
 
      System.out.println(tableHeaderEles.get(i).text()); 
 
     } 
 
     System.out.println(); 
 

 
     Elements tableRowElements = tableElements.select("tr"); 
 

 
     for (int i = 0; i < tableRowElements.size(); i++) { 
 
      Element row = tableRowElements.get(i); 
 
      System.out.println("row"); 
 
      Elements rowItems = row.select("td"); 
 
      for (int j = 0; j < rowItems.size(); j++) { 
 
       System.out.println(rowItems.get(j).text()); 
 
      } 
 
      System.out.println(); 
 
     } 
 

 
     } catch (IOException e) { 
 
     e.printStackTrace(); 
 
     } 
 
    } 
 
}

答えて

0

ないJavaはDEVのすべてではないテーブルから特定のものを取得したいと、しかし、ここに行く...

最後のループでは、画面上のすべてのtd要素のリストである 'rowItems'があります。このため、このデータを確実に検索して、欲しいです。私はあなたがこれらのテーブルを制御できないだろうと推測しているので、IDで検索できるようにトラッキングしているIDにIDを設定するだけでは不十分です。

フォーマットが常に同じ場合、 'rowItems'の項目をインデックスでどのように引き出すかを理解してください。あなたが戻るたびにインデックスは同じデータでなければなりません。うまくいけば、それは正しい方向に向けるでしょう!

+0

"Javaデベロッパーではありませんが、実際には..."とはどういう意味ですか? また、あなたが言っていることの例を示すことができますか? –

関連する問題