Java Jsoup网络抓取
我正在尝试使用jSoup来抓取一个具有以下内容的网站。我对jSoup很陌生,现在仍在努力解决这个问题。我想做的是能够将产品名称和价格放入excel文件中,名称在A列,价格在B列,0.00可以忽略,也可以放在C列,只要更简单。任何帮助都会很好,因为我知道有人会问,这不是家庭作业。Java Jsoup网络抓取,java,web-scraping,jsoup,Java,Web Scraping,Jsoup,我正在尝试使用jSoup来抓取一个具有以下内容的网站。我对jSoup很陌生,现在仍在努力解决这个问题。我想做的是能够将产品名称和价格放入excel文件中,名称在A列,价格在B列,0.00可以忽略,也可以放在C列,只要更简单。任何帮助都会很好,因为我知道有人会问,这不是家庭作业。 提前谢谢,我真的很感激 <tr> <td class="sku" width="40" align="center">AAN13097</td> <
提前谢谢,我真的很感激
<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 -->
AAN13097
这应该可以做到。然而,您发布的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);
网页抓取可能违反某些网站的使用条款。你确定你有这样做的权限吗?是的,我使用的是一家供应商,他们没有技术人员,因此他们没有csv文件中的项目导入我的订单管理软件。这是表格元素吗,因为这是列表前的“表格”代码
<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 </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>
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);