Saket Sinha Saket Sinha - 3 months ago 15
Java Question

I need to use jsoup selector for more than one p tags

Hi i have an html code suppose,

<P>This is Stack overflow</p>
<p>---------------</p>
<p>---------------</p>
<p>This is stack overflow2</p>


I need to select all the
<p>
tags from the
<p>
tag which contain "This is Stack overflow" . until the
<p>
tag which contain "This is stack overflow2".
is it possible to use jsoup because i am using it from java class.

Thanks In advance

Tim Tim
Answer

Not sure what exactly you are requesting, but this is a start:

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class JsoupTester {
    public static void main(String[] args) throws Exception {
        String html = "<P>This is Stack overflow</p>"
                + "<p>---------------</p>"
                + "<p>---------------</p>"
                + "<p>This is stack overflow2</p>";
        Document doc = Jsoup.parse(html);
        Elements elements = doc.select("p");

        for(Element e : elements) {
            System.out.println(e.text());
        }
    }
}

Now you can adapt the loop and add conditions, depending on what you want to include.

These links are helpful:

Comments