gpt4 book ai didi

java - Jsoup 网页抓取

转载 作者:行者123 更新时间:2023-12-01 04:54:25 25 4
gpt4 key购买 nike

我正在尝试使用 jSoup 来抓取具有以下内容的网站。我对 jSoup 很陌生,仍在尝试弄清楚。我想做的是能够获取产品名称和价格并将它们放入 Excel 文件中,名称在 A 列中,价格在 B 列中,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);

关于java - Jsoup 网页抓取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14411981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com