Jsoup:在某个元素之前获取所有元素/在某个元素之后移除所有堆叠的元素 [英] Jsoup: get all elements before a certain element / remove all stacked elements after a certain element

查看:81
本文介绍了Jsoup:在某个元素之前获取所有元素/在某个元素之后移除所有堆叠的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题还有另一个版本,我将用它来建立我的

This question has another version that I'm going to use to base mine Jsoup: get all elements before a certain element / remove all elements after a certain element

我想获取.friend-pets之前的所有.pet.我尝试使用原始问题中提出的解决方案,但在此用例中遇到了这个结果.

I want to get all .pet that are before the .friend-pets. I tried using the solution proposed in the original question but I encounter this result for this use case.

输入:

<div class="pets">
  <div>
    <div class="pet">1</div>
    <div class="pet">2</div>
  </div>
    <div class="pet">3</div>
    <div class="friends-pets">Your friends have these pets:</div>
    <div class="pet">4</div>
  <div>
     <div class="pet">5</div>
     <div class="pet">6</div>
  </div>
<div>

预期:

<div class="pet">1</div>
 <div class="pet">2</div>
 <div class="pet">3</div>

实际:

<div class="pet">1</div>
<div class="pet">2</div>
<div class="pet">3</div>
<div class="pet">5</div>
<div class="pet">6</div>

当我跑步时会发生这种情况:

This happens when I run:

Element petsWrapper = document.selectFirst(".pets");
Elements pets = petsWrapper.select(".pet");
// select middle element
Element middleElement = petsWrapper.selectFirst(".friends-pets");
// remove from "pets" every element that comes after the middle element
pets.removeAll(middleElement.nextElementSiblings());
System.out.println(pets);

因为nextSiblings()方法仅获取属于同一父级的元素.当我使用第二个答案中建议的css选择器时,如下所示:

Because nextSiblings() method only gets elements that belong to the same parent. When I use css selectors like suggested in the 2nd answer like this:

.pet:not(.friends-pets ~ .pet)

我收到此错误:

Did not find balanced marker at '.friends-pets ~ .pet'

所以我不能真正测试它是否真的有效.

So I can't really test if it actually works.

谢谢.

推荐答案

我的方法是使用一个选择器选择您想要的内容和不需要的内容.您可以使用逗号 加入选择器,这样它就可以作为 AND 运算符使用.元素的顺序将保持不变,您将拥有一个相同级别"的所有元素的列表.没有父母.然后,您只能获得该列表的前半部分.

My approach would be to select what you want and what you don't want with one selector. You can join selectors using comma , so it will work as AND operator. Order of elements will be kept and you will have one list of all elements "at the same level" without parents. Then you can get only the first half of that list.

Elements goodElementsWithBadElement = document.select(".pet,.friends-pets");
Element badElement = goodElementsWithBadElement.select(".friends-pets").first();
int positionOfBadElement = goodElementsWithBadElement.indexOf(badElement);
List<Element> onlyWhatYouWant = goodElementsWithBadElement.subList(0, positionOfBadElement);
System.out.println(onlyWhatYouWant);

顺便说一句,我是前一个答案的作者;)

btw I was the author of that previous answer ;)

这篇关于Jsoup:在某个元素之前获取所有元素/在某个元素之后移除所有堆叠的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆