Jsoup 选择并替换多个 元素

发布于 2025-01-08 17:40:50 字数 1243 浏览 0 评论 0原文

所以我只是尝试 Jsoup API 并有一个简单的问题。我有一个字符串,希望保持字符串完好无损,除非通过我的方法传递。我希望字符串通过这个方法并取出包裹链接的元素。现在我有:

public class jsTesting {
public static void main(String[] args) {
    String html = "<p>An <a href='http://example.com/'><b>example</b></a> link and after that is a second link called <a href='http://example2.com/'><b>example2</b></a></p>";
    Elements select = Jsoup.parse(html).select("a");
    String linkHref = select.attr("href");
    System.out.println(linkHref);       
}}

这仅返回第一个未包装的 URL。我希望所有 URL 以及原始字符串都解开。提前致谢

编辑:解决方案:

非常感谢您的回答,我只对其进行了轻微编辑以获得我想要的结果。这是我正在使用的完整解决方案:

public class jsTesting {
public static void main(String[] args) {
    String html = "<p>An <a href='http://example.com/'><b>example</b></a> link and after that is a second link called <a href='http://example2.com/'><b>example2</b></a></p>";
    Document doc = Jsoup.parse(html);
    Elements links = doc.select("a[href]");
    for (Element link : links) {
        doc.select("a").unwrap();
    }
    System.out.println(doc.text());
}

}

再次感谢

So I am just trying out the Jsoup API and have a simple question. I have a string and would like to keep the string in tact except when passed through my method. I want the string to pass through this method and take out the elements that wrap the links. Right now I have:

public class jsTesting {
public static void main(String[] args) {
    String html = "<p>An <a href='http://example.com/'><b>example</b></a> link and after that is a second link called <a href='http://example2.com/'><b>example2</b></a></p>";
    Elements select = Jsoup.parse(html).select("a");
    String linkHref = select.attr("href");
    System.out.println(linkHref);       
}}

This returns the first URL unwrapped only. I would like all URLs unwrapped as well as the original string. Thanks in advance

EDIT: SOLUTION:

Thanks alot for the answer and I edited it only slightly to get the results I wanted. Here is the solution in full that I am using:

public class jsTesting {
public static void main(String[] args) {
    String html = "<p>An <a href='http://example.com/'><b>example</b></a> link and after that is a second link called <a href='http://example2.com/'><b>example2</b></a></p>";
    Document doc = Jsoup.parse(html);
    Elements links = doc.select("a[href]");
    for (Element link : links) {
        doc.select("a").unwrap();
    }
    System.out.println(doc.text());
}

}

Thanks again

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尽揽少女心 2025-01-15 17:40:50

这是更正后的代码:

public class jsTesting {
    public static void main(String[] args) {
        String html = "<p>An <a href='http://example.com/'><b>example</b></a> link and after that is a second link called <a href='http://example2.com/'><b>example2</b></a></p>";
        Elements links = Jsoup.parse(html).select("a[href]"); // a with href;
        for (Element link : links) {
            //Do whatever you want here
            System.out.println("Link Attr : " + link.attr("abs:href"));
            System.out.println("Link Text : " + link.text());    
        }       
    }
}

Here's the corrected code:

public class jsTesting {
    public static void main(String[] args) {
        String html = "<p>An <a href='http://example.com/'><b>example</b></a> link and after that is a second link called <a href='http://example2.com/'><b>example2</b></a></p>";
        Elements links = Jsoup.parse(html).select("a[href]"); // a with href;
        for (Element link : links) {
            //Do whatever you want here
            System.out.println("Link Attr : " + link.attr("abs:href"));
            System.out.println("Link Text : " + link.text());    
        }       
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文