Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
执行页面后保存页面的html输出';s javascript_Javascript_Phantomjs_Headless Browser - Fatal编程技术网

执行页面后保存页面的html输出';s javascript

执行页面后保存页面的html输出';s javascript,javascript,phantomjs,headless-browser,Javascript,Phantomjs,Headless Browser,有一个网站我正试图抓取,它首先加载一个html/js 使用js修改表单输入字段,然后发布。 如何获得发布页面的最终html输出 我试着用phantomjs实现这一点,但它似乎只有一个选项来渲染图像文件。谷歌搜索表明这应该是可能的,但我不知道怎么做。我的尝试: var page = require('webpage').create(); var fs = require('fs'); page.open('https://www.somesite.com/page.aspx', function

有一个网站我正试图抓取,它首先加载一个html/js 使用js修改表单输入字段,然后发布。 如何获得发布页面的最终html输出

我试着用phantomjs实现这一点,但它似乎只有一个选项来渲染图像文件。谷歌搜索表明这应该是可能的,但我不知道怎么做。我的尝试:

var page = require('webpage').create();
var fs = require('fs');
page.open('https://www.somesite.com/page.aspx', function () {
    page.evaluate(function(){

    });

    page.render('export.png');
    fs.write('1.html', page.content, 'w');
    phantom.exit();
});
这段代码将用于客户端,我不能期望他安装太多的包(nodejs、casperjs等)


谢谢

当我直接复制了您的代码,并将URL更改为www.google.com时,它运行良好,保存了两个文件:

  • 1.html
  • export.png

请记住,文件将被写入运行脚本的位置,而不是.js文件所在的位置

我想到的一种方法是,除了使用无头浏览器外,显然是模拟ajax调用,并按请求集成页面发布过程。。但是,这通常有点棘手,应该作为最后的手段使用,除非您真的喜欢深入研究javascript代码。

这可以通过一些php代码和javascript轻松完成。 使用fopen()和fwrite() 并使用此功能保存它:
var generatedSource=new XMLSerializer().serializeToString(文档)

我尝试了几种类似的方法,使用Selenium获得了最好的结果

在我尝试幻影和幻影之前。Phantom在页面上执行JS时经常崩溃。

我正在使用PhantomJS运行测试。我在函数中添加了以下代码:

var require = patchRequire(require);
var fs = require('fs');

casper.test.begin("My Test", {
    tearDown: function(){
        casper.capture("export.png");
        fs.write("1.html", casper.getHTML(undefined, true), 'w');
    },
    test: function(test){
        // test code

        casper.run(function(){
            test.done();
        });
    }
});

请参阅文档了解和。

您的输出代码是正确的,但存在同步性问题。在页面加载完成之前正在执行的输出行。 您可以连接到onLoadFinished回调以了解何时发生这种情况。请参阅下面的完整代码

    var page = new WebPage()
    var fs = require('fs');

    page.onLoadFinished = function() {
      console.log("page load finished");
      page.render('export.png');
      fs.write('1.html', page.content, 'w');
      phantom.exit();
    };

    page.open("http://www.google.com", function() {
      page.evaluate(function() {
      });
    });

当使用像谷歌这样的网站时,这可能是一种欺骗,因为它的加载速度如此之快,以至于你经常可以像现有的那样内联执行屏幕抓图。在phantomjs中,计时是一件棘手的事情,有时我会使用setTimeout进行测试,看看计时是否有问题

经过两天漫长的挣扎和挫折,我终于解决了类似的问题。其中的一个例子说明了这个技巧。开心点

"use strict";

function waitFor(testFx, onReady, timeOutMillis) {
    var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000, //< Default Max Timout is 3s
        start = new Date().getTime(),
        condition = false,
        interval = setInterval(function() {
            if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
                // If not time-out yet and condition not yet fulfilled
                condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
            } else {
                if(!condition) {
                    // If condition still not fulfilled (timeout but condition is 'false')
                    console.log("'waitFor()' timeout");
                    phantom.exit(1);
                } else {
                    // Condition fulfilled (timeout and/or condition is 'true')
                    console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
                    typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
                    clearInterval(interval); //< Stop this interval
                }
            }
        }, 250); //< repeat check every 250ms
};


var page = require('webpage').create();

// Open Twitter on 'sencha' profile and, onPageLoad, do...
page.open("http://twitter.com/#!/sencha", function (status) {
    // Check for page load success
    if (status !== "success") {
        console.log("Unable to access network");
    } else {
        // Wait for 'signin-dropdown' to be visible
        waitFor(function() {
            // Check in the page if a specific element is now visible
            return page.evaluate(function() {
                return $("#signin-dropdown").is(":visible");
            });
        }, function() {
           console.log("The sign-in dialog should be visible now.");
           phantom.exit();
        });
    }
});
“严格使用”;
函数waitFor(testFx、onReady、timeOutMillis){
var maxtimeOutMillis=timeOutMillis?timeOutMillis:3000,//<默认最大超时为3s
开始=新日期().getTime(),
条件=假,
间隔=设置间隔(函数(){
如果((新日期().getTime()-start
您必须使用Python吗?Java是一个选项吗?您也可以使用
document.outerHTML
获取页面内容,那么图像和样式呢?