0

我在 JAVA 中使用 JSOUP DOM 解析器在URL http://www.walmart.com/search/search-ng.do?tab_value=all&search_query=camera&search_constraint=0&Find=Find&ss=false&ic=16_32报废沃尔玛页面。

我正在基于用户参数构建 URL 并使用构建 DOM 对象

Document doc = Jsoup.parse(contentVar); 

对于下一步,我想打印所有产品/价格。我使用了以下代码:

String price = doc.getElementsByClass("camelPrice").text();
String title = doc.getElementsByClass("ListItemLink").text();       
System.out.println("Product: " + title);
System.out.println("Price: "+ price);

在这里,我使用价格和产品描述的标签。但是我的结果是:

标题/产品名称:C1, C2, ... C16(c 是相机标题)
价格:$ 279.95 $ 279.95 $ 479.00 $ 479.00 $ $ 60.00 $ 60.00 $ 99.00 $ 99.00 429.00 $ 429.00 $ 129.00 $ 129.00 $ 109.00 $ 109.00 $ 89.00 $ 89.00 $ 384.00 $ 384.00 $ $ 69.00 $ 69.00 279.00 $ 279.00 $ 129.00 $ 129.00 $ 55.20  -  $ $ 69.00 55.20  -  $ $ 69.00:74.00 $:74.00 $ $ 119.00 119.00

由于可能的快速查看标签,此处的价格重复。有没有办法使用任何 JSOUP 方法来消除价格的重复

4

2 回答 2

2

好吧,看到 html dom,我注意到在有价格的意义上存在重复

<div class="ItemShelfAvail">     <----------- SEE HERE
<div class="OnlinePriceAvail">
<div class="PriceHeader OnlineHead">Online</div>
<div class="PriceContent">
<div class="PriceDisplay" id="price_display_23204350_2">
<div class="PriceCompare">
<div class="camelPrice"><span class="prefixPriceText2"></span><span class="bigPriceText2">$279.</span><span class="smallPriceText2">00</span><span></span></div>

和一个价格

<div class="OnlinePriceAvail">
<div class="PriceHeader OnlineHead">Online</div>
<div class="PriceContent">
<div class="PriceDisplay" id="price_display_23204350_2">
<div class="PriceCompare">
<div class="camelPrice"><span class="prefixPriceText2"></span><span class="bigPriceText2">$279.</span><span class="smallPriceText2">00</span><span></span></div>

您必须从两者中查看您想要的列表,然后放置适当的选择器。如果您想要它们两个,只需获取 getElementsByClass 返回的元素列表并操纵每个价格。

getElementsByClass 返回 Elements,这是一个列表,其中每个节点都是 Element 类型。你可以做

Elements elPrice = doc.getElementsByClass("camelPrice");
于 2013-03-22T07:31:09.620 回答
1

我知道这对线程的创建者现在可能没用,但我在查看如何在英国亚马逊上查找产品价格时发现了这一点。

String pricing = doc.getElementsByClass("priceLarge").text();
System.out.println("price : " + pricing);

这是执行此操作的代码:)

于 2013-06-02T18:50:05.703 回答