2011-10-01 5 views
2

나는 JS0p를 사용하여 웹 페이지를 this과 같이 구문 분석하고 두 개의 문자열 배열로 만듭니다. 각 문자열은 ListActivity) 그리고 링크를위한 하나. 이 텍스트 값 중 일부는 jsoup가 구문 분석에 문제가있는 특수 문자를 가지고 있습니다. 처음에는 다음과 같이 사용했습니다 :예기치 않은 값 (안드로이드)을 반환하는 Jsoup Element.attr()

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() 메서드를 적용했을 때, 실제로 "링크를 반환했을 때"스위치를 눌렀을 때만 반환되었습니다. 나는 그것이 정적으로 어떤 것을 envoking하는 것과 관련이있을 것이라고 생각합니다. – Hihaatje