且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-01 22:53:58

输出代码你是正确的,但同步性存在问题。在页面加载完成之前,您正在执行输出行。
您可以绑定onLoadFinished回调以查明何时发生。请参阅下面的完整代码。

the output code you have is correct, but there is an issue with synchronicity. The output lines that you have are being executed before the page is done loading. You can tie into the onLoadFinished Callback to find out when that happens. See full code below.

    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测试时间是否有问题。

When using a site like google, it can be deceiving because it loads so quicker, that you can often execute a screengrab inline like you have it. Timing is a tricky thing in phantomjs, sometimes I test with setTimeout to see if timing is an issue.