java
  • parsing
  • jsoup
  • 2012-02-23 44 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

    相關問題