簡體   English   中英

Jsoup獲取具有CDATA標簽的javascript內容?

[英]Jsoup get contents of javascript that has CDATA tags?

我正在使用Jsoup來解析網頁。 但是有些我希望解析的信息是在CDATA標記內部,這會阻止解析器提取內部數據。 我如何從CDATA標簽中提取數據? 例:

<script type='text/javascript'><!--// <![CDATA[
    OA_show('300x250');
// ]]> --></script>
         <script type='text/javascript'>alert("Hello");</script>

如果我使用Jsoup解析此頁面並嘗試使用“script [type = text / javascript]”選擇頁面中所有匹配的元素,我會返回頁面中沒有CDATA標簽而不是警報的其他腳本的內容(“你好”); 值。 我如何使用Jsoup在CDATA標簽中獲取該值?

謝謝!

String page = "<script type='text/javascript'><!--// <![CDATA[\n" +
        "    OA_show('300x250');\n" +
        "// ]]> --></script>\n" +
        "         <script type='text/javascript'>alert(\"Hello\");</script>";

String html = Jsoup.parse(page).select("script").get(0).html();
html = html.replace("<!--// <![CDATA[", "");
html = html.replace("// ]]> -->", "");

System.out.println(html);

結果

OA_show('300x250');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM