转储包含iframe的页面的HTML [英] Dump HTML of page including iframes

查看:141
本文介绍了转储包含iframe的页面的HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转储网页的HTML内容,包括< iframe> 元素中包含的iframe的HTML。 Chrome开发人员工具元素标签能够显示以这种方式嵌入的iframe。



当我说转储HTML内容时,我对浏览器自动化工具感兴趣像Selenium或PhantomJS。是否有这些工具内置了这种容量?



例如,我想要的此页面应包含的HTML源代码这个嵌入式页面

解决方案

您可以使用phantomjs来实现这个功能

以下是幻影js服务器代码的代码片段。


$ b

  var system = require('system'); 
var url = system.args [1] || ;
if(url.length> 0){
var page = require('webpage')。create();
page.open(url,function(status){
if(status =='success'){
var delay,checker =(function(){
var html = page.evaluate(function(){
var body = document.getElementsByTagName('body')[0];
if(body.getAttribute('data-status')=='ready'){
return document.getElementsByTagName('html')[0] .outerHTML;
}
});
if(html){
clearTimeout(delay);
console.log(html);
phantom.exit();
}
});
delay = setInterval(checker,100);
}
});
}

在html上使用data-status属性让phantomjs知道当页面准备好了,如果html属于你。如果html页面不属于你,另一个选择是使用一个很好的超时。


I'd like to dump the HTML contents of a web page, including the HTML of iframes included inside the <iframe> elements. The Chrome Developer Tools "Elements" tab is capable of showing iframe embedded in this way.

When I say "dump the HTML contents" I'm interested in browser automation tools like Selenium or PhantomJS. Do any of these tools have this capacity built in?

For example, the HTML dump I'd like of this page should include the HTML source of this embedded page.

解决方案

You can use phantomjs to achieve this

Here is a code snippet from the phantom js server code.

var system = require('system');
var url = system.args[1] || '';
if(url.length > 0) {
  var page = require('webpage').create();  
  page.open(url, function (status) {
    if (status == 'success') {
      var delay, checker = (function() {
        var html = page.evaluate(function () {
          var body = document.getElementsByTagName('body')[0];
          if(body.getAttribute('data-status') == 'ready') {
            return document.getElementsByTagName('html')[0].outerHTML;
          }
        });
        if(html) {
          clearTimeout(delay);
          console.log(html);
          phantom.exit();
        }
      });
      delay = setInterval(checker, 100);
    }
  });
}

on the html you use the "data-status" attribute to let phantomjs know when the page is ready if the html belongs to you . The other option would be to use a nice timeout if the html page does not belong to you.

这篇关于转储包含iframe的页面的HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆