使用jQuery在iframe中提取元素

时间:2018-07-24 06:54:32

标签: jquery iframe scrape

我正在尝试使用jquery提取iframe中的视频链接。我一直没有结果。

这是我的代码:

    <html>
        <head></head>
        <body>
             <iframe id="video" src="//content.jwplatform.com/players/Jd4BqNM0-xkVaWyDw.html" width="500" height="500" frameborder="0" scrolling="auto" allowfullscreen style="position:absolute;"></iframe>

            <script type="text/javascript" src="js/jQuery.js"></script>
             <script>   
alert($('#video').contents().find("meta[property='og:video']").attr('content'));
             </script>


        </body>
     </html>

1 个答案:

答案 0 :(得分:1)

尝试这样: https://jsfiddle.net/NwrtE/62/

$(document).ready(function () {

$.ajax({
      type: 'GET', 
        url: $("iframe#video").attr("src"),
      dataType: 'html',
      success: function(data) {

        //cross platform xml object creation from w3schools
        try //Internet Explorer
          {
          xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
          xmlDoc.async="false";
          xmlDoc.loadXML(data);
          }
        catch(e)
          {
          try // Firefox, Mozilla, Opera, etc.
            {
            parser=new DOMParser();
            xmlDoc=parser.parseFromString(data,"text/xml");
            }
          catch(e)
            {
            alert(e.message);
            return;
            }
          }

        var metas = xmlDoc.getElementsByTagName("meta");
        for (var i = 0; i < metas.length; i++) {   
          if (metas[i].getAttribute("property") == 'og:video') {
            alert(metas[i].getAttribute("content"));
          }
        }
        //adjust above line appropriately
      }
  });


  });