phantomJS网页超时

Kim*_*sen 31 webpage-rendering phantomjs

我已经设置了一个脚本来创建我们的应用程序的webshots.它运行完美,一切正常直到我遇到一个破坏网址的图像:

 "<img src='http://testserver.our.intranet/fetch/image/373e8fd2339696e2feeb680b765d626e' />"
Run Code Online (Sandbox Code Playgroud)

我已经设法在使用下面的6秒后打破脚本,之前它只是永远循环.

但是,是否可以忽略网络请求(AKA取出图像DOM),然后继续创建没有图像的拇指,(或者注入的图像缺少图像!)

var page = require('webpage').create(),
    system = require('system'),
    address, output, size;

if (system.args.length < 3 || system.args.length > 5) {
    phantom.exit(1);
} else {
    address = system.args[1];
    output  = system.args[2];
    page.viewportSize = { width: 640, height: 640 };
    page.zoomFactor = 0.75;
    page.clipRect = { top: 10, left: 0, width: 640, height: 490 };
    try{
        page.open(address, function (status) {
            if (status !== 'success') {
                console.log('Unable to load the address!');
                phantom.exit();
            } else {
                window.setTimeout(function () {
                    page.render(output);
                    phantom.exit();
                }, 200);
            }
        });    
    } finally{
        setTimeout(function() {
            console.log("Max execution time " + Math.round(6000) + " seconds exceeded");
            phantom.exit(1);
        }, 6000);
    }
}
Run Code Online (Sandbox Code Playgroud)

Mic*_*ord 62

PhantomJS 1.9引入了一个新设置,resourceTimeout它可以控制请求在取消之前需要多长时间.除此之外,还有一个onResourceTimeout事件在请求超时时触发.

这是一个代码片段,说明了上述所有内容:

var page = require('webpage').create();  
page.settings.resourceTimeout = 5000; // 5 seconds
page.onResourceTimeout = function(e) {
  console.log(e.errorCode);   // it'll probably be 408 
  console.log(e.errorString); // it'll probably be 'Network timeout on resource'
  console.log(e.url);         // the url whose request timed out
  phantom.exit(1);
};

page.open('http://...', function (status) {
...
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,这些选项现在很难记录.我必须通过GitHub 讨论PhantomJS源代码才能找到它们.

  • resourceTimeout是否应该终止被调用的phantomjs进程.因为对我来说,它没有.phantomjs进程只是无限期挂起. (2认同)