鉴于以下情况:
<li class="med grey mkp2">
<a href="http://www.amazon.com/gp/offer-listing/B004N627KS/ref=sr_1_1_olp?ie=UTF8&qid=1357396214&sr=8-1&keywords=097855073358&condition=new"><span class="price bld">$28.15</span> new <span class="grey">(14 offers)</span></a> </li>
我需要获取 href,这听起来很简单,对吧?但是,我可以找到要从中获取的正确列表项的唯一方法是获取 <span class="price bld">
,所以我需要的 href 在它之前。它类似于 Extracting href from a class within other div/id classes with jsoup , 但反过来。
css 类“med grey mkp2”可以有很多列表项,但我只需要来自那些带有 class="price bld"的标注范围的内容。
我怎样才能做到这一点?
最佳答案
只能选择目标元素(<a>
),不能选择子元素(<span>
),否则只会返回<span>
。元素。在这种特殊情况下,您可以使用 :has()
选择器检查目标元素是否具有所需的子元素。
Elements elements = document.select("a:has(.price.bld)");
另见:
-
:has(seletor)
: find elements that contain elements matching the selector; e.g.div:has(p)
关于java - JSoup - 如何获取紧接在 <span class = *> 之前的 href(url/link)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14367358/