2013-01-19 145 views
1

我想用jSoup刮一個網站,有以下幾點。我對jSoup非常陌生,我仍然試圖弄清楚。我想要做的是能夠獲取產品名稱和價格,並將它們放入一個excel文件中,A列中的名稱和B列中的價格,0.00可以被忽略或放置在C列中,無論哪種方式更簡單。任何幫助都會很好,因爲我知道有人會問,這不是一項家庭作業。
在此先感謝,我真的很感激。Jsoup網絡抓取

<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> 
+1

Web抓取可能違背了某些網站的使用條款。你確定你有權限這麼做嗎? – Swapnil

+0

是的,它是我使用的供應商,他們沒有技術人員,因此他們沒有將csv文件中的項目導入我的訂單管理軟件。 – user1992433

回答

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); 
+0

這是否是表格元素,因爲這是列表之前的「表格」代碼 – user1992433