How I can replace “text” in the each tag using Jsoup

后端 未结 3 1106
粉色の甜心
粉色の甜心 2021-01-22 09:54

I have the following html:





    

text text

3条回答
  •  醉梦人生
    2021-01-22 10:31

        String html = " ...";
        Document doc = Jsoup.parse(html);
        Elements p = doc.select("div#content > p");
        p.html(p.html().replaceAll("text", "word"));
        System.out.println(doc.toString());
    

    div#content > p means that the elements

    in the element

    which id is content.

    If you want to replace the text only in text:

        Elements p = doc.select("div#content > p > strong");
        p.html(p.html().replaceAll("text", "word"));
    

提交回复
热议问题