java
  • parsing
  • jsoup
  • 2012-02-23 46 views 0 likes 
    0

    所以我只是想出来的Jsoup API,并有一个简单的问题。我有一个字符串,并希望保持字符串不间断,除非通过我的方法。我希望字符串通过这个方法并取出包装链接的元素。现在我有:Jsoup选择和更换多个<a>元素

    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和原始字符串一样解开。在此先感谢

    编辑: SOLUTION:很多的答案

    谢谢,我编辑它只是稍微得到我想要的结果。这是在充分的解决方案,我使用:

    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()); 
    } 
    

    }再次

    感谢

    回答

    3

    这里的更正后的代码:

    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());  
         }  
        } 
    } 
    
    +0

    相反link.attr的'(“ABS: href“)'你最好使用'link.absUrl(”href“)'。 – BalusC 2012-02-23 17:08:00

    +0

    非常感谢,非常感谢。然而,使用unwrap()方法没有办法做到这一点吗? – 2012-02-23 17:24:11

    相关问题