如何使用jsoup解析

How to parse using jsoup

给予

<div class="alert_right">
<p>welcome!<script>setTimeout("window.location.href ='index.php';", 1000);</script></p></div>

如何使用 Jsoup 获取 welcome! 文本?

如果你的HTMLString中,你可以使用下面的代码:

Document doc = Jsoup.parse(HTML);
Elements div =  doc.select(".alert_right > p:nth-child(1)");
String s = div.text();

欢迎s

以下是获取 welcome! 文本的另一种方法。

StringBuffer myHTML = new StringBuffer();
myHTML.append("<div class=\"alert_right\"><p>welcome!" +
            "<script>setTimeout(\"window.location.href =\'index.php\';\", 1000);</script>" +
            "</p></div>");
Document myDoc = Jsoup.parse(myHTML.toString());
//get first child of div
String result = myDoc.select("div.alert_right").get(0).text();
System.out.println(result);