2016-08-30 200 views
1

有解析的java jsoup解析如何解析HTML

<a href="/summoner/userName=Huhi" class="Link">Huhi</a> 

在HTML中任何可能的方式:

<a href="/summoner/userName=Huhi" class="Link">Huhi</a> 
<a href="/summoner/userName=Huhi" class="Link">White</a> 
<a href="/summoner/userName=Huhi" class="Link">Angle</a> 

輸出:

Huhi 
White 
Angle 
+0

'select'每個'了'並打印其'文本( )'? – Pshemo

回答

1

創建文檔,並讓所有的[href]鏈接,遍歷這些鏈接並獲取它們包含的文本。像這樣:

Document doc = Jsoup.connect(url).get(); 
Elements links = doc.select("a[href]"); 
for (Element link : links) { 
    String text = link.text(); 
} 
1

您只需選擇a和迭代elements和打印

String html ="<a href=\"/summoner/userName=Huhi\" class=\"Link\">Huhi</a>\n" + 
       "<a href=\"/summoner/userName=Huhi\" class=\"Link\">White</a>\n" + 
       "<a href=\"/summoner/userName=Huhi\" class=\"Link\">Angle</a>"; 

     Document doc = Jsoup.parse(html); 
     Elements links = doc.select("a"); 
     for (Element link : links) { 
      System.out.println(link.text()); 

     } 

更多參考檢查此鏈接selector-syntax

+0

我已經得到了這一點,現在我有這個問題.. http://stackoverflow.com/questions/39238695/java-jsoup-remove-new-lines/39240666?noredirect=1#comment65818367_39240666 – nooby