尝试使用jsoup解析的是以下Numérod'arrêt:5216和NuméroNOR:63066,但似乎无济于事,任何建议将不胜感激:

`

<div class="1st">
<p>
<h3>Numérotation : </h3>
Numéro d'arrêt : 5216
<br />
Numéro NOR : 63066
</div>


`

更新:

我让此代码正常工作,但它不断给我这个异常org.jsoup.nodes.Element无法转换为org.jsoup.nodes.TextNode:

Document tunisie = Jsoup.connect("http://www.juricaf.org/arret/TUNISIE-COURDECASSATION-20060126-5216").get();

for (Element titres : tunisie.select("div.arret")){

                    String titre = titres.select("h1#titre").first().text();
                    System.out.println(titre);
                    System.out.println("\n");
                }


                for (Element node : tunisie.select("h3")) {
                    TextNode numérodarrêt = (TextNode) node.nextSibling();
                    System.out.println(" " + numérodarrêt.text());
                    System.out.println("\n");

                }

                //NuméroNOR et Identifiant URN LEX
                for (Element element2 : tunisie.select("br")) {
                    TextNode NuméroNOR_IdentifiantURNLEX = (TextNode) element2.nextSibling();
                    System.out.println(" " + NuméroNOR_IdentifiantURNLEX.text());
                    System.out.println("\n");
                }


更新:

这是下面试图解析图像的链接。

Parsing text outside html tags

最佳答案

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

public class JsoupTest {

    public static void main(String[] args) throws IOException {
       Document tunisie = Jsoup.connect("http://www.juricaf.org/arret/TUNISIE-COURDECASSATION-20060126-5216").get();
       // get the first div in class arret
       Element arret = tunisie.select("div.arret").first();
       // select h1 tag by its ID to get the title
       String  titre = arret.select("#titre").text();
       System.out.println(titre);
       // to get the text after h3 select h3 and go to next sibling
       String txtAfterFirstH3    = arret.select("h3").first().nextSibling().toString();
       System.out.println(txtAfterFirstH3);
       // select first br by its index; note first br has the index 0; and call nextSibling to get the text after the br tag
       String txtAfterFirstBr    = arret.getElementsByTag("br").get(0).nextSibling().toString();
       System.out.println(txtAfterFirstBr);
       // the same as above only with next index
       String txtAfterSecondBr    = arret.getElementsByTag("br").get(1).nextSibling().toString();
       System.out.println(txtAfterSecondBr);
    }
}

关于java - Jsoup将Element强制转换为TextNode导致异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43095468/

10-12 21:40