簡體   English   中英

Jsoup Web抓取

[英]Jsoup web scraping

我正在嘗試使用jSoup抓取具有以下內容的網站。 我對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>

這應該做。 但是,您發布的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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM