2011-10-01 44 views
2

我使用JSoup來解析一個網頁,如this,並將它變成兩個字符串數組,每個項目的文本值一個(要顯示在ListActivity )和一個鏈接。其中一些文本值具有jsoup解析時遇到的特殊字符。起初我使用:Jsoup Element.attr()返回意外的值(Android)

Document doc = Jsoup.connect(URL).get(); 
maintable = doc.select(".kader").first(); 

獲取與該內容表的元素。在這裏另一個線程,有人說這是可行的使用Jsoup.parse(HTML),所以我改成了這樣:

Document doc = Jsoup.connect(URL).get(); 
Document DOC = Jsoup.parse(doc.html()); 
if(doc.select(".kader") != null){ 
    maintable = DOC.select(".kader").first(); 
} 

然而,這似乎並沒有擦出火花。所以我把它留作稍後要解決的問題(這裏也許),但這不是我的主要問題。 如果我試圖讓所有的主要內容顯示,我會使用此方法的鏈接的字符串數組:

public String[] getTranslationLinks(){ 
    String[] items = new String[alllinks.size()]; 
    Element tempelement; 
    for(int i = 0;i<items.length;i++){ 
     tempelement = alllinks.get(i); 

     items[i] = tempelement.attr("abs:href"); 
    } 
    return items; 
} 

調試說tempelement包含正確的元素,但由於某些原因,.attr(」 abs:href「)不按要求返回鏈接。例如包含:

<a href="./vertaling.php?id=6518" target="_top" title="">Hoofdstuk 3, tekst A: Herakles de slaaf</a> 

但是.attr(abs:href)返回「」。

做任何你知道的方法來解決這些問題?

回答

1

最好的辦法是創建一個小的可編譯和可運行的代碼來演示你的問題,SSCCE。例如,當我根據我對問題的解釋創建我的SSCCE時,它似乎起作用。這是代碼:

import java.io.IOException; 
import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 
import org.jsoup.select.Elements; 

public class Kader { 
    private static final String MAIN_URL = "http://www.latijnengrieks.com/categorie.php?id=120"; 
    private static final String ALL_LINKS = "a[href]"; 
    private static Element maintable; 

    public static void main(String[] args) { 
     Document jsDoc = null; 

     try { 
     jsDoc = Jsoup.connect(MAIN_URL).get(); 
     maintable = jsDoc.select(".kader").first(); 

     Elements alllinks = maintable.select(ALL_LINKS); 

     String[] translationLinks = getTranslationLinks(alllinks); 

     for (String tLink : translationLinks) { 
      System.out.println(tLink); 
     } 

     } catch (IOException e) { 
     e.printStackTrace(); 
     } 
    } 

    public static String[] getTranslationLinks(Elements alllinks){ 
     String[] items = new String[alllinks.size()]; 
     Element tempelement; 
     for(int i = 0;i<items.length;i++){ 
      tempelement = alllinks.get(i); 

      items[i] = tempelement.attr("abs:href"); 
     } 
     return items; 
    } 
} 

這是輸出:

http://www.latijnengrieks.com/vertaling.php?id=5586 
http://www.latijnengrieks.com/profiel.php?id=1 
http://www.latijnengrieks.com/vertaling.php?id=6342 
http://www.latijnengrieks.com/profiel.php?id=1 
http://www.latijnengrieks.com/vertaling.php?id=6159 
http://www.latijnengrieks.com/profiel.php?id=1 
http://www.latijnengrieks.com/vertaling.php?id=5368 
http://www.latijnengrieks.com/profiel.php?id=11 
http://www.latijnengrieks.com/vertaling.php?id=5371 
http://www.latijnengrieks.com/profiel.php?id=11 
http://www.latijnengrieks.com/vertaling.php?id=5797 
http://www.latijnengrieks.com/profiel.php?id=1 
http://www.latijnengrieks.com/vertaling.php?id=6310 
http://www.latijnengrieks.com/profiel.php?id=1 
http://www.latijnengrieks.com/vertaling.php?id=5799 
http://www.latijnengrieks.com/profiel.php?id=1 
http://www.latijnengrieks.com/vertaling.php?id=5776 
http://www.latijnengrieks.com/profiel.php?id=1 
http://www.latijnengrieks.com/vertaling.php?id=5861 
http://www.latijnengrieks.com/profiel.php?id=1 
http://www.latijnengrieks.com/vertaling.php?id=5521 
http://www.latijnengrieks.com/profiel.php?id=1 
http://www.latijnengrieks.com/vertaling.php?id=5622 
http://www.latijnengrieks.com/profiel.php?id=1 
http://www.latijnengrieks.com/vertaling.php?id=5692 
http://www.latijnengrieks.com/profiel.php?id=1 
http://www.latijnengrieks.com/vertaling.php?id=6367 
http://www.latijnengrieks.com/profiel.php?id=1 
http://www.latijnengrieks.com/vertaling.php?id=5910 
http://www.latijnengrieks.com/profiel.php?id=1 
http://www.latijnengrieks.com/vertaling.php?id=6011 
http://www.latijnengrieks.com/profiel.php?id=1 
http://www.latijnengrieks.com/vertaling.php?id=5940 
http://www.latijnengrieks.com/profiel.php?id=1 
http://www.latijnengrieks.com/vertaling.php?id=6009 
http://www.latijnengrieks.com/profiel.php?id=1 
http://www.latijnengrieks.com/vertaling.php?id=5573 
http://www.latijnengrieks.com/profiel.php?id=1 
http://www.latijnengrieks.com/vertaling.php?id=5572 
http://www.latijnengrieks.com/profiel.php?id=1 
http://www.latijnengrieks.com/vertaling.php?id=5778 
http://www.latijnengrieks.com/profiel.php?id=1 
http://www.latijnengrieks.com/vertaling.php?id=5993 
http://www.latijnengrieks.com/profiel.php?id=1 
http://www.latijnengrieks.com/vertaling.php?id=5623 
http://www.latijnengrieks.com/profiel.php?id=1 
http://www.latijnengrieks.com/vertaling.php?id=5642 
http://www.latijnengrieks.com/profiel.php?id=1 
http://www.latijnengrieks.com/vertaling.php?id=6000 
http://www.latijnengrieks.com/profiel.php?id=1 
http://www.latijnengrieks.com/vertaling.php?id=5798 
http://www.latijnengrieks.com/profiel.php?id=1 
http://www.latijnengrieks.com/vertaling.php?id=5578 
http://www.latijnengrieks.com/profiel.php?id=1 
http://www.latijnengrieks.com/vertaling.php?id=6415 
http://www.latijnengrieks.com/profiel.php?id=14 
+0

感謝您的幫助。你的代碼工作,但我仍然覺得很奇怪,雖然attr方法應用於相同的元素,但它仍然提供了不同的結果。 – Hihaatje

+0

@Hihaatje:請解釋一下,因爲我不知道你的意思是不同的結果。 –

+0

當我使用我的代碼將.attr()方法應用於元素時,它只返回「」,然後當我切換它時,它實際上返回鏈接。我認爲這可能與靜態地煽動一些事情有關。 – Hihaatje