Jsoup - 在下载页面文本时分离所有 url

Jsoup - seprate all url while download text of a page

下载网页时如何在 jsoup 中使用删除所有 link。

我使用下面的代码给我一个网页的文本

public static void Url(String urlTosearch) throws IOException {
        URL = urlTosearch;
        Document doc = Jsoup.connect(URL).get();
         String textOnly = Jsoup.parse(doc.toString()).text();
        Output ob = new Output();
        ob.Write(textOnly);

    }

但是有什么方法可以让我在下载页面

的文本时将所有 link 分开

我会做类似的事情:

public static void Url (String urlTosearch) throws IOException {
    URL = urlTosearch;
    Document doc = Jsoup.connect(URL).get();

    // Take all links in the page
    Elements links = doc.select("a[href]");
    for (Element link : links) { // Iter on each links to get URL
        String relHref = link.attr("href"); // Get relative URL
        String absHref = link.attr("abs:href"); // Get absolute URL
        // I let you do whatever you want with urls
    }

}

How can i use in jsoup to remove all the link while downloading a webpage

您可以 select 所有具有 href 属性的 a 元素和 remove 它来自 Document 表示页面 DOM 结构的对象.

所以你的代码可以看起来像

Document doc = Jsoup.connect(URL).get();
doc.select("a[href]").remove();//remove all found `<a href...>` elements from DOM
String textOnly = doc.text();//generate text from DOM without your links