Jsoup没有用HTML解析IFrame

Dam*_*ien 2 java jsoup

任何人都可以解释为什么jsoup没有在以下的HTML中获取iframe

<div class="video">
<script class="video_preview_source" type="text/html">
<iframe src="//player.vimeo.com/video/109fdsagfa" id="campaign_video_7566" width="353" height="240" frameborder="0"></iframe></script>
<div class="video_preview"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

用这个代码

Document document = Jsoup.parse(html);

Elements elements = document.select("div.video script.video_preview_source iframe[src]");

System.out.println("elements:" + elements);
Run Code Online (Sandbox Code Playgroud)

ahu*_*us1 5

我认为它没有拿起,<iframe />因为它不期望<script />标签内的HTML .你需要.data()来返回内容.

另请注意:您无法直接选择属性,您将始终获得一个完整的元素作为回报.

拆分所有这些,以下代码适用于我:

Document document = Jsoup.parse(html);

Elements elements =
            document.select("div.video script.video_preview_source");

Document iframeDoc = Jsoup.parse(elements.get(0).data());

Elements iframeElements = iframeDoc.select("iframe");

System.out.println(iframeElements.attr("src"));
Run Code Online (Sandbox Code Playgroud)

此致,亚历山大.