获取连续包含两个空格的类(JSoup)

时间:2015-10-14 17:08:41

标签: java android jsoup

我试图通过它的课程得到一个特定的div。该类实际上包含多个用空格分隔的类,但是:最后一个类分隔为空格!

Ex:class = test [SPACE] test [SPACE] test [SPACE] [SPACE] test

完整:

  

列表[SPACE] category_templates [SPACE] clearfix [SPACE] shelfListing [SPACE] [SPACE] multiSaveListing

现在我是如何继续这样做的?

无法正常工作(无错误):

Elements divItemContainer = doc.select("div[class=listing category_templates clearfix shelfListing  multiSaveListing]");
            for (Element div : divItemContainer) {
                Toast.makeText(ApplicationContextProvider.getContext(), "Got Div: ", Toast.LENGTH_SHORT).show();
            }

无效(抛出错误:字符串不能包含空格):

Elements divItemContainer = doc.select("div.listing.category_templates.clearfix.shelfListing..multiSaveListing");
            for (Element div : divItemContainer) {
                Toast.makeText(ApplicationContextProvider.getContext(), "Got Div: ", Toast.LENGTH_SHORT).show();
            }

无效(无错误):

Elements divItemContainer = doc.select("div.listing.category_templates.clearfix.shelfListing.multiSaveListing");
            for (Element div : divItemContainer) {
                Toast.makeText(ApplicationContextProvider.getContext(), "Got Div: ", Toast.LENGTH_SHORT).show();
            }
PS:Toast旨在故意让App崩溃!除了杀戮之外什么也没做,而且应该发生(至少在目前)

Source

<div class="listing category_templates clearfix shelfListing  multiSaveListing"><div id="yousaveImage"></div><div class="multisave" id="multiSaveId"><a class="linksave" href="/promotion/2-for-250/ls85559"><span class="view-all">View all</span><span class="offer-2for3">2 for</span><span><span class="poundSign"></span><span class="ping-offer-finalValue">£2.50</span><span class="ping-offer-finalValue-1" style="display:none"></span><span class="pencep" style="display:none">p</span></span></a></div><div class="container"><div class="slider category_templates"><input id="itemId" value="1000000476716" type="hidden"><input id="maxQtyId" value="24.0" type="hidden"><div class="product active"><div class="slider"><div class="information active"><div class="imgContainer"><a href="/product/no-added-sugar/asda-chosen-by-you-orange-pineapple-double-strength-squash/1000000476716"><img class="" src="http://ui2.assets-asda.com:80/g/v5/501/375/5051413501375_130_IDShot_4.jpeg" data-original="http://ui2.assets-asda.com:80/g/v5/501/375/5051413501375_130_IDShot_4.jpeg" alt="ASDA Chosen By You Orange &amp; Pineapple Double Strength Squash 2 FOR £2.50" title="" onerror="loadNoImage(this)"></a><a href="#" id="addToShoppingList" class="shoppinglist-icon icon-addto" title="Add to shopping list" aria-haspopup="true" aria-expanded="false"><span class="accessible"> Add to shopping list</span></a></div><p class="bundle-contains" style="display:none;"> Contains <span>0</span>&nbsp;<span>items</span></p><p class="subTitle">1.5LT</p></div></div><div class="product-content"><span class="bundle-banner" style="display:none;"> Bundle </span><span class="promoBanner"></span><span class="primaryBanner" style="display:none;"><a href="/promotion/2-for-250/ls85559" title="2 FOR £2.50">2 FOR £2.50</a></span><span class="title" id="productTitle"><a role="presentation" aria-hidden="true" tabindex="-1" href="/product/no-added-sugar/asda-chosen-by-you-orange-pineapple-double-strength-squash/1000000476716" title="ASDA Chosen By You Orange &amp; Pineapple Double Strength Squash"><span>ASDA Chosen By You Orange &amp; Pineapple Double Strength Squash</span></a></span><div class="product-type-icons" style="visibility:visible"><i data-contentid="" data-similarproducts="true" data-title="Suitable for Vegetarians" data-name="Suitable for Vegetarians" title="Vegetarian" class="type-icon icon-suitable-for-vegetarians" data-infoiconid="1215398078196" data-id="2854136"><a href="#" class="icons">Vegetarian</a></i></div><div class="rating-static rating-50"><span class="star star1"></span><span class="star star2"></span><span class="star star3"></span><span class="star star4"></span><span class="star star5"></span></div><div class="prod-limit-Mask"></div><div class="quantity-info-Mask"><span class="qLimit-toolTip"></span><a href="#" class="undoLimit-close" role="alertdialog" aria-labelledby="quantityLimitText"> Close </a><div class="qLimit-popUp"><p id="quantityLimitText"><span class="qLimit-Sorry">Sorry...</span>You can't add more than <span class="max-qty-val">24</span> per order</p></div></div><div id="cartBground" class="addedbg"><div class="price-cart-block"><div class="price-wrap category_templates"><span class="price"><span>£1.40</span></span><span class="priceInformation"> (9.3p/100ml) </span></div><a href="#" onclick="return false;" class="btn btn-primary addItemToTrolley fav-test-item " data-skuid="1000000476716">Add</a><a href="/product/no-added-sugar/asda-chosen-by-you-orange-pineapple-double-strength-squash/1000000476716" class="view-bundle btn btn-primary fav-bundle-test-item" style="display:none;" data-skuid="1000000476716">View bundle</a><div class="quantityOptions clearfix"><a href="#" onclick="return false;" id="minusButtonId" onselectstart="return false;" aria-label="Quantity Decrease" class="btn btn-primary button-quantity quantityDecrease"><span>–</span></a><input aria-label="Quantity in your trolley" value="1" name="quantityInTrolley" class="prd-txt" maxlength="5" type="number"><a href="#" onclick="return false;" id="plusButtonId" onselectstart="return false;" aria-label="Quantity Increase" class="btn btn-primary button-quantity quantityIncrease"><span>+</span></a><a href="#" onclick="return false;" class="btn btn-primary add-small" style="display:none;" id="applyItemToTrolley">Add</a><div id="qtySelect" class="qty-wrapper" style="display: none;"><div class="qty-select"><span class="qty-value" tabindex="0" title="Quantity">Q<span class="accessible">uanti</span>ty</span><span class="qty-select-icon"></span></div><ul class="qty-list" style="display:none"><li class="qtyAccessible"><span title="Quantity" data-salesunit="Qty">Q<span class="accessible">uanti</span>ty</span></li><li class="kgAccessible"><span title="Kilogram" data-salesunit="kg">k<span class="accessible">ilo</span>g<span class="accessible">ram</span></span></li></ul></div><p id="inTrolleyId">in your trolley</p></div></div><div id="itemAjaxLoader" class="ajaxLoader 1000000476716" style="display:none;"><img src="//ui3.assets-asda.com/theme/img/common/loader.svg" style="width: 32px;" onerror="this.src=//ui3.assets-asda.com/theme/img/common/ajax-loader.gif; this.onerror=null;"></div><div class="unavail-item-message"> Item unavailable<span class="qLimit-toolTip"></span></div><div class="unavail-item"><span class="unavailable-image"></span><span></span></div></div></div><div class="sectionMenu"></div></div></div></div></div>

1 个答案:

答案 0 :(得分:1)

这有效,但它不安全,没有理由使用它。此外,为了使您能够工作,类和空格的顺序必须相同。你说它没有,但我已经测试了它,它确实如此。

Elements divItemContainer = doc.select("div[class=listing category_templates clearfix shelfListing  multiSaveListing]");
for (Element div : divItemContainer) {
    Toast.makeText(ApplicationContextProvider.getContext(), "Got Div: ", Toast.LENGTH_SHORT).show();
}

这是做到这一点的方法。类的顺序并不重要,也不是空格。你说它不起作用,但我已经对它进行了测试,确实如此。

Elements divItemContainer = doc.select("div.listing.category_templates.clearfix.shelfListing.multiSaveListing");
for (Element div : divItemContainer) {
    Toast.makeText(ApplicationContextProvider.getContext(), "Got Div: ", Toast.LENGTH_SHORT).show();
}

对于这个错误是正确的。

Elements divItemContainer = doc.select("div.listing.category_templates.clearfix.shelfListing..multiSaveListing");
for (Element div : divItemContainer) {
    Toast.makeText(ApplicationContextProvider.getContext(), "Got Div: ", Toast.LENGTH_SHORT).show();
}

您的查询在执行之前会经过验证。发生的验证将您输入的每个类作为参数。您输入的css选择器会针对每个.进行拆分,并且通过键入连续的.,您将创建空类。

public static void notEmpty(String string) {
    if ((string == null) || (string.length() == 0))
        throw new IllegalArgumentException("String must not be empty");
}

它不起作用的原因不是你的选择者。尝试键入从服务器获得的响应。当您Document doc = Jsoup.parse()...尝试打印doc时。这是否包含您要搜索的元素?我怀疑它没有。

如果我正确地说你正在寻找的元素不在你得到的答案中,那么你有两种可能性。

  1. 服务器将您的程序视为bot并且不允许该程序或它为您提供移动设备页面,因此它可以为您提供浏览浏览器时所看到的内容。如果是这种情况,则解决方案是设置userAgent
  2. 该元素不存在,因为它是由javascript生成的。 Jsoup只是一个解析器,而不是一个浏览器。它无法执行javascript,因此无法生成动态内容。为了检查您需要的内容是否是动态的,只需导航到该页面并按 Ctrl + U 并检查您需要的元素是否在那里。这是执行任何javascript之前的内容。