2012-01-26 37 views
0

這裏是我用來從html代碼片段訪問「test」的代碼片段。我如何從html中訪問URL https://www.google.com無法在href(jSoup)中返回文本

Elements e = doc.getElementsByAttribute("href"); 
Iterator<Element> href = e.iterator(); 
    while (href.hasNext()){ 
    Element link = href.next(); 
    String text = link.text(); 
    } 



    <a href="javascript:linkToExternalSite('https://www.google.com','','61x38pxls','','','','','')">Test</a> 

回答

0

的HREF是你可以用Jsoup的元素attr方法訪問屬性。這給了你屬性的全部內容,當然,你需要一些模式匹配來檢索URL。

1

我不是Jsoup專家,但Jsoup是一個html解析器,你不能用它來解析javascript標籤中的內容。

所以,你的方法應該是使用Jsoup提取

"javascript:linkToExternalSite('https://www.google.com','','61x38pxls','','','','','')"

比使用regular expressions獲取內容/網址。

0
String html = "<a href=\"javascript:linkToExternalSite('https://www.google.com','','61x38pxls','','','','','')\">Test</a>"; 
    Document doc = Jsoup.parse(html); 
    Element e = doc.select("a[href]").first(); 
    String href = e.attr("href"); 
    String arg[] = href.split("'"); 
    String url = arg[1]; 
    // Output: 'https://www.google.com' 
    System.out.println(url);