2014-03-04 49 views
0

我正在閱讀網頁中的內容,然後使用Jsoup解析器幫助解析它,以僅獲取正文部分中存在的超鏈接。我得到的輸出:從Java中獲取給定字符串的子串

<a href="/sports/sports.asp" style="TEXT-DECORATION: NONE"><font color="#0000FF">Sports</font></a> 
<a href="/titanic/titanic.asp" style="TEXT-DECORATION: NONE"><font color="#0000FF">Titanic</font></a> 
<a href="gastheft.asp" onmouseover="window.status='License Plate Theft';return true" onmouseout="window.status='';return true">license plates</a> 
<a href="miracle.asp" onmouseover="window.status='Miracle Cars';return true" onmouseout="window.status='';return true">miracle cars</a> 
<a href="/crime/warnings/clear.asp" onmouseover="window.status='Clear Loss';return true" onmouseout="window.status='';return true" target="clear">Clear</a> 

and even more hyperlinks. 

從所有的人,所有我感興趣的是像

/sports/sports.asp 
/titanic/titanic.asp 
gastheft.asp 
miracle.asp 
/crime/warnings/clear.asp 

我怎樣才能做到這一點使用字符串或有任何其他方式或方法將數據使用Jsoup Parser本身提取這些信息?

+1

http://jsoup.org/cookbook/extracting-data/attributes-text-html – helderdarocha

回答

0

試試這個,它可以幫助

String html = "<p>An <a href='http://example.com/'><b>example</b></a> link.</p>"; 
Document doc = Jsoup.parse(html); 
Element link = doc.select("a").first(); 

String text = doc.body().text(); // "An example link" 
String linkHref = link.attr("href"); // "http://example.com/" 
String nextIndex = linkHref .indexOf ("\"", linkHref); 
+0

謝謝哥們..這是我尋找的完美答案:) –

+0

@ user3326925我們歡迎您 – Engineer

0

這應該是使用

String.indexOf 

parsign的基本比特作爲

index = jsoupOutput.indexOf ("href=\""); 

nextIndex = jsoupOutput.indexOf ("\"", index); 

在適當位置必要的檢查。

0

讓我們假設字符串錨包含這些鏈接之一,則子將HREF =後」開始索引和結束索引將是指數9這種方式後的第一個引號:

String anchor = "<a href=\"/sports/sports.asp\" style=\"TEXT-DECORATION: NONE\"><font color=\"#0000FF\">Sports</font></a>"; 
int beginIndex = anchor.indexOf("href=\"") + 6; //To start after <a href=" 
int endIndex = anchor.indexOf("\"", beginIndex); 
String desiredPart = anchor.substring(beginIndex, endIndex); 

而且就是這樣,如果錨的形狀將永遠是這樣..更好的選擇是使用正則表達式和最好將使用的XML解析器。

0

以此爲參考

import java.util.regex.*; 

public class HelloWorld{ 

    public static void main(String []args){ 

     String s = "<a href=\"/sports/sports.asp\" style=\"TEXT-DECORATION: NONE\"><font color=\"#0000FF\">Sports</font></a>"+ 
        "<a href=\"/titanic/titanic.asp\" style=\"TEXT-DECORATION: NONE\"><font color=\"#0000FF\">Titanic</font></a>"+ 
        "<a href=\"gastheft.asp\" onmouseover=\"window.status='License Plate Theft';return true\" onmouseout=\"window.status='';return true\">license plates</a>"+ 
        "<a href=\"miracle.asp\" onmouseover=\"window.status='Miracle Cars';return true\" onmouseout=\"window.status='';return true\">miracle cars</a>"+ 
        "<a href=\"/crime/warnings/clear.asp\" onmouseover=\"window.status='Clear Loss';return true\" onmouseout=\"window.status='';return true\" target=\"clear\">Clear</a>"; 
     Pattern p = Pattern.compile("href=\".+?\""); 
     Matcher m = p.matcher(s); 
     while(m.find()) 
     { 
      System.out.println(m.group().split("=")[1].replace("\"","")); 
     } 

    } 
} 

輸出

/sports/sports.asp 
/titanic/titanic.asp 
gastheft.asp 
miracle.asp 
/crime/warnings/clear.asp 
2

你可以試試這個,它的作品。

public class AttributeParsing { 

/** 
* @param args 
*/ 
public static void main(String[] args) { 
    final String html = "<a href=\"/sports/sports.asp\" style=\"TEXT-DECORATION: NONE\"><font color=\"#0000FF\">Sports</font></a>"; 

    Document doc = Jsoup.parse(html, "", Parser.xmlParser()); 
    Element th = doc.select("a[href]").first(); 

    String href = th.attr("href"); 

    System.out.println(th); 
    System.out.println(href); 
} 

}

輸出:

日:<a href="/sports/sports.asp" style="TEXT-DECORATION: NONE"><font color="#0000FF">Sports</font></a>

HREF:/sports/sports.asp

0

你能做到在同一行:

String[] paths = str.replaceAll("(?m)^.*?\"(.*?)\".*?$", "$1").split("(?ms)$.*?^"); 

第一個方法調用除去每行中除目標外的所有內容,第二個方法調用拆分新行(將在所有OS終止符上運行)。

FYI (?m)打開「多行模式」,(?ms)也打開「dotall」標誌。

相關問題