JSoup如何在数组中存储元素?

时间:2015-07-07 16:40:31

标签: java arrays jsoup elements

我正在制作一个擦除亚马逊页面的java程序。当有人输入搜索查询并显示结果页面时,我的程序将获取所有结果价格。我已经知道如何找到这些价格并将它们打印到控制台。例如,假设用户搜索了术语" spoon",我输入以下代码:

Elements prices = doc.select("span.s-price");
    System.out.println("Prices: " + prices);

然后我收到以下输出:

Prices: <span class="a-size-base a-color-price s-price a-text-bold">CDN$ 7.99</span>
<span class="a-size-base a-color-price s-price a-text-bold">CDN$ 4.24</span>
<span class="a-size-base a-color-price s-price a-text-bold">CDN$ 12.00</span>
<span class="a-size-base a-color-price s-price a-text-bold">CDN$ 9.99</span>
<span class="a-size-base a-color-price s-price a-text-bold">CDN$ 22.99</span>
<span class="a-size-base a-color-price s-price a-text-bold">CDN$ 10.00</span>
<span class="a-size-base a-color-price s-price a-text-bold">CDN$ 5.94</span>
<span class="a-size-base a-color-price s-price a-text-bold">CDN$ 4.99</span>
<span class="a-size-base a-color-price s-price a-text-bold">CDN$ 4.79</span>
<span class="a-size-base a-color-price s-price a-text-bold">CDN$ 5.99</span>
<span class="a-size-base a-color-price s-price a-text-bold">CDN$ 15.48</span>
<span class="a-size-base a-color-price s-price a-text-bold">CDN$ 4.07</span>
<span class="a-size-base a-color-price s-price a-text-bold">CDN$ 5.88</span>
<span class="a-size-base a-color-price s-price a-text-bold">CDN$ 12.99</span>
<span class="a-size-base a-color-price s-price a-text-bold">CDN$ 6.71</span>
<span class="a-size-base a-color-price s-price a-text-bold">CDN$ 11.99</span>

但是,我不知道如何将这些字符串存储在一个数组中?我更具体地说,只是喜欢CDN $ double&#39;值,虽然我想我知道如何处理这个问题。我如何将这些字符串存储为数组?

以下是我感兴趣的Amazon.ca源代码部分的代码(冗长而凌乱,我的道歉):

<div id="center">


<div id="atfResults" class="a-row s-result-list-parent-container"><ul id="s-results-list-atf" class="s-result-list s-col-1 s-col-ws-1 s-result-list-hgrid s-height-equalized s-list-view s-text-condensed"><li id="result_0" data-asin="B00X08P338" class="s-result-item"><div class="s-item-container"><div class="a-fixed-left-grid"><div class="a-fixed-left-grid-inner" style="padding-left:160px"><div class="a-fixed-left-grid-col a-col-left" style="width:160px;margin-left:-160px;_margin-left:-80px;float:left;"><div class="a-row"><div class="a-column a-span12 a-text-center"><a class="a-link-normal a-text-normal" href="http://www.amazon.ca/HIC-Harold-Cocktail-Stainless-12-Inch/dp/B00X08P338/ref=sr_1_1?ie=UTF8&amp;qid=1436286484&amp;sr=8-1&amp;keywords=spoon"><img alt="Product Details" src="http://ecx.images-amazon.com/images/I/21TcgyjDH%2BL._AA160_.jpg" onload="viewCompleteImageLoaded(this, new Date().getTime(), 16, false);" class="s-access-image cfMarker" height="160" width="160"></a></div></div></div><div class="a-fixed-left-grid-col a-col-right" style="padding-left:2%;*width:97.6%;float:left;"><div class="a-row a-spacing-small"><a class="a-link-normal s-access-detail-page  a-text-normal" title="HIC Harold Import Bar Cocktail Mixing Spoon, 18/8 Stainless Steel, 12-Inch" href="http://www.amazon.ca/HIC-Harold-Cocktail-Stainless-12-Inch/dp/B00X08P338/ref=sr_1_1?ie=UTF8&amp;qid=1436286484&amp;sr=8-1&amp;keywords=spoon"><h2 class="a-size-medium a-color-null s-inline s-access-title a-text-normal">HIC Harold Import Bar Cocktail Mixing <strong>Spoon</strong>, 18/8 Stainless Steel, 12-Inch</h2></a><div class="a-row a-spacing-none"><span class="a-size-small a-color-secondary">by </span><span class="a-size-small a-color-secondary">HIC Harold Import Co.</span></div></div><div class="a-row"><div class="a-column a-span7"><div class="a-row a-spacing-none"><a class="a-link-normal a-text-normal" href="http://www.amazon.ca/HIC-Harold-Cocktail-Stainless-12-Inch/dp/B00X08P338/ref=sr_1_1?ie=UTF8&amp;qid=1436286484&amp;sr=8-1&amp;keywords=spoon"><span class="a-size-base a-color-price s-price a-text-bold">CDN$ 7.99</span></a>

2 个答案:

答案 0 :(得分:0)

不完全是最干净的解决方案,但它应该有效。基本上元素只是一个元素列表。

    ArrayList<Element> elements = new ArrayList<>();

    for (Element e : prices) {
        elements.add(e);
    }

    Element[] elementArr = elements.toArray(new Element[]{});

答案 1 :(得分:0)

你可以这样做。

Elements elements = d.select("span.s-price");
String[] prices = new String[elements.size()];
for (int i = 0; i < elements.size(); i++) {
    prices[i] = elements.get(i).text();
}