Jsoup Element.attr()返回意外值(Android)

时间:2011-10-01 13:07:21

标签: java android jsoup

我正在使用JSoup来解析像this这样的网页,并将其分成两个字符串数组,一个用于每个项目文本值(将在ListActivity中显示),另一个用于链接。其中一些文本值具有jsoup难以解析的特殊字符。起初我用过:

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();
}
然而,这似乎也没有用。所以我把它留作后来要解决的东西(也许这里),但这不是我的主要问题。 如果我尝试获取主要内容中显示的所有链接的String数组,我将使用此方法:

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;
}

调试器说templement包含正确的元素,但由于某种原因,.attr(“abs:href”)不会按要求返回链接。例如,温度包含:

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

但.attr(abs:href)返回“”。

你们中有谁知道解决这些问题的方法吗?

1 个答案:

答案 0 :(得分: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