javajsoup

Jsoup Remove the link href attribute starts with Href


I want to remove the link href attribute starts with http://example.com or https://example.com.

Below is my code, but it is not exactly no error message.

I do not know what should I do. Please help me.

public class NewClass {

    public static void main(String[] args) {
        try {
            Document connect = Jsoup.connect("http://example.vn/ngoc-trinh-duoc-goi-la-hinh-mau-bao-hieu-tot-doi-dep-dao.html").get();
            Elements selects = connect.select("div.remain_detail");
            String levels = "a[href^=http://example.com],a[href^=https://example.com]";
            for (String level : levels.split(",")) {
                selects.remove(level);
            }
            System.out.println(Jsoup.clean(selects.html(), Whitelist.relaxed()));
        } catch (IOException ex) {
            Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

Solution

  • use regex to select and remove from selects:

    selects.select("a[href~=(http|https)://example.com]").remove()