1

jSoup を使用して、次の Web サイトをスクレイピングしようとしています。私はjSoupに非常に慣れていないので、まだ理解しようとしています。私がしたいのは、製品名と価格を取り、それらを列 A に名前、列 B に価格を含む Excel ファイルに入れることです。0.00 は無視するか、列 C に配置する方が簡単です。 . これは宿題ではありません。
よろしくお願いします。

<tr>
        <td class="sku" width="40" align="center">AAN13097</td>
        <td class="productName" width="440"><a name="<!-- Empty field [Field4]  -->"></a> 
                                American Antler Dog Chew Large (40-60 lb Dogs)                                          </td>
        <!--<td id="weight_816">0</td>-->
        <td class="quantity" width="20" align="center">
            <input type="text" name="816:qnty" id="qnty_816" class="inputQuantity">
            <input type="checkbox" name="itemnum" value="816" id="itemnum_816" class="itemnum">
        </td>
        <!--<td class="extWeight" id="extWeight_816">0.0</td>-->
        <td width="80" align="center" id="price_816">$9.70</td>
        <td width="120" align="center" class="extPrice" id="extPrice_816">$0.00</td>
    </tr>
                                                                                                                <!-- rec 815 -->

<tr>
        <td class="sku" width="40" align="center">AAN13096</td>
        <td class="productName" width="440"><a name="<!-- Empty field [Field4]  -->"></a> 
                                American Antler Dog Chew Medium (20-40 lb Dogs)                                         </td>
        <!--<td id="weight_815">0</td>-->
        <td class="quantity" width="20" align="center">
            <input type="text" name="815:qnty" id="qnty_815" class="inputQuantity">
            <input type="checkbox" name="itemnum" value="815" id="itemnum_815" class="itemnum">
        </td>
        <!--<td class="extWeight" id="extWeight_815">0.0</td>-->
        <td width="80" align="center" id="price_815">$7.15</td>
        <td width="120" align="center" class="extPrice" id="extPrice_815">$0.00</td>
    </tr>

**これはリストの前の「テーブル」コードであるため、これはテーブル要素になりますか?そうでない場合、htmlコードで何を探す必要がありますか?

<table border="0" cellpadding="8" cellspacing="0" id="orderForm" width="700">
<thead>
<tr>
<th width="40px" align="center">Line</th>
<th width="420" align="center">Item description&nbsp;</th>
<th width="40px" align="center">Quantity</th>
<th width="80px" align="center">Unit Price</th>
<th width="120px" align="center">Amount</th>
</tr>
</table><div class="tableCont"><table border="0" cellpadding="8" cellspacing="0"    
id="orderForm" width="700" height="350px">
<tbody>                                                                                                           
<!-- rec 1638 -->
<a name="1638"></a>
4

1 に答える 1

0

これでうまくいくはずです。ただし、投稿した HTML には tr のテーブルの親が含まれていませんでした。もちろん、このコードが機能するには HTML に含まれている必要があります。そうしないと、Jsoup が tr/td 要素をドロップし、コードが機能しません。

Document doc = Jsoup.parse(html); // html attribute should contain tr elements HTML content
String productName = doc.select("tr .productName").first().text(); // Get name
Element extPriceElement = doc.select("tr td.extPrice").first();
String id = extPriceElement.id().replaceAll("extPrice_", ""); // Get id     
String productPrice = doc.select("tr #price_" + id).first().text(); // Get price
String productExtPrice = extPriceElement.text(); // Get ext price
System.out.println("Product name : " + productName);                
System.out.println("Price : " + productPrice);
System.out.println("Ext price : " + productExtPrice);
于 2013-01-19T11:13:41.570 に答える