J4C3N-14
J4C3N-14

Reputation: 696

Get the content of all tags AFTER THE FIRST of the SAME tags - Jsoup

Trying to gather some website content using jsoup and have run into a problem with the collection of some information. What I would like to do is gather the content of all the p tags after the first of x amount of p tags ( x being an unknown number could be 1-10):

enter image description here

I know that you can collect the first like this:

.select("div#news_content_wide > p").first(); 

but is there a way to select the content AFTER the first without knowing the amount you want?

Thanks

Upvotes: 2

Views: 85

Answers (2)

Edward J Beckett
Edward J Beckett

Reputation: 5140

You can make a function taking the target div as a parameter and the paragraph element you don't want to return... I.E. '1' (actually zero)...

<body>

<script>

      function removeElement( div, num ) {
        var parent = document.getElementById( div );
        var returnElems = [];
        if( parent.children.length > 1 && parent.children[0].nodeType === 1 ) {
            if( parent.children[0].tagName === "P") {
                var paras = parent.children;

                for( var i = 0; i < paras.length; i++) {
                    if( i != (num - 1) ) {
                        returnElems.push(paras[i]);
                    }

                }
            }
        }
          return returnElems;

      }

  </script>

<div id="news_content_wide">
    <p>DON'T RETURN THIS</p>
    <p></p>
    <p></p>
    <p></p>
    <p></p>
    <p></p>
    <p></p>
    <p></p>
    <p></p>
    <p></p>
    <p></p>
    <p></p>
    <p></p>
    <p></p>
    <p></p>
    <p></p>
    <p></p>
    <p></p>
    <p></p>
    <p></p>
</div>
</body>

CONSOLE

var elems = removeElement("news_content_wide", 1);
console.log(elems);

Upvotes: 0

Musa
Musa

Reputation: 97707

You can try div#news_content_wide > p~p it uses the general sibling combinator

What happens here is that any p who has a sibling p precedence it will be selected, so basically all but the first.

Upvotes: 2

Related Questions