通过类在一行中包含两个空格获取div (JSoup)



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

交货:类=测试(空间)测试(空间)测试(空间)(空间)测试

完整

:

清单(空间)category_templates(空间)clearfix(空间)shelfListing(空间)(空间)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崩溃的!它什么也不做,只是杀死,这应该发生(至少目前)

源:

<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>

这可以工作,但它是不安全的,没有理由使用它。此外,为了使其工作,类和空白的顺序必须相同。你说没有,但我测试过了,确实有。

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并检查您需要的元素是否在那里。

最新更新