使用PhantomJS不会在循环中多次尝试呈现PNG [英] PNG is not being rendered using PhantomJS with multiple attempts in a loop

查看:144
本文介绍了使用PhantomJS不会在循环中多次尝试呈现PNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var page = require('webpage')。create(); 
var filepath =/ home / abc /+abc.html; //创建路径charts.html
for(i = 0; i <3; i ++){//三次尝试
console.log(hi)
page.open (filepath,function(status){
console.log(status);
// if(status!=='success'){//如果不成功,再次尝试
/ / continue;
//}

page.render('abc.png');
phantom.exit();
});
}
phantom.exit();

运行如下代码:

  $ phantomjs hello.js 

Q1:期待 abc.png 在我的主目录下生成。但是,对于当前的代码,它不会生成。



Q2:如果将块注释掉,则会导致代码执行停滞。为什么这样?

解决方案

您有三个交织在一起的问题。 > 1。 继续

继续用于转到下一个对或循环结构进行迭代,如,同时。这不是你在这里做的。在本地范围内 continue 没有任何循环,这意味着这是一个语法错误。



在PhantomJS 2不再显示语法错误,并简单地冻结。运行你最喜欢的linter(jslint,jshint,eslint)来防止这样的错误。

2。 page.open()是异步的



您无法使用循环打开多个页面。 page.open()是异步的。通过调用它,你只能启动负载。由于它不会在下一次迭代时阻塞,因此您将使用新的URL覆盖请求,并且第一个请求将永远不会完成,因为for循环比页面请求快得多。

唯一可能被加载的页面是最后一页,但是...

3。提前退出



您现在过早退出。由于 page.open()是异步的,因此循环将完全运行,而不会完全加载一个页面。最后你有一个 phantom.exit(),它将立即退出PhantomJS。






如果您必须打开一堆页面,请使用递归方法或巧妙使用 setTimeout()。您也可以使用异步的 series()函数
$ b $ p

示例作为一个简单的递归函数:

 

code> var page = require('webpage')。create();
var filepath =/ home / abc /+abc.html; //创建到charts.html的路径
function run(i){
if(i <= 0){
return phantom.exit();

page.open(filepath,function(status){
console.log(status);
if(status ==='success'){// if not ('abc'+ i +'。png');
}
run(i-1);
});
}
run(3);


var page = require('webpage').create();
var filepath = "/home/abc/" + "abc.html"; //create path to charts.html    
for (i = 0; i < 3; i++) { // make three attempts
    console.log("hi")
    page.open(filepath, function(status) {
      console.log(status);
      //if(status !== 'success') { //if not success, make another attempt
//        continue;
  //    }

      page.render('abc.png');
      phantom.exit();
    });
}    
phantom.exit();

running this code like this:

$ phantomjs hello.js

Q1: I am expecting abc.png to be generated in my home dir. However with current code it is not generated.

Q2: Adding the commented out if block back causes the code execution to become stuck. Why so ?

解决方案

You have three problems that are intertwined.

1. continue

continue is used to go to the next iteration of a loop construct such as for or while. That's not what you're doing here. There is no loop around continue in the local scope which means this is a syntax error.

There is a bug in PhantomJS 2 that doesn't show Syntax errors anymore and simply freezes. Run your favorite linter (jslint, jshint, eslint) to prevent such mistakes.

2. page.open() is asynchronous

You cannot use a loop to open multiple page one after another. page.open() is asynchronous. By calling it, you only initiate the load. Since it doesn't block on the next iteration you will overwrite the request with a new URL and the first request will never be completed, because a for-loop is much faster than a page request.

The only page that could potentially be loaded is the last one, but ...

3. Premature exiting

You're exiting too early. Since page.open() is asynchronous, the loop will run completely through without loading even one page completely. At the end you have a phantom.exit() which will exit PhantomJS immediately.


If you have to open a bunch of pages, use a recursive approach or a clever use of setTimeout(). You could also use the async's series() function to write it in a more sane way.

Example as a simple recursive function:

var page = require('webpage').create();
var filepath = "/home/abc/" + "abc.html"; //create path to charts.html    
function run(i){
    if (i <= 0) {
      return phantom.exit();
    }
    page.open(filepath, function(status) {
      console.log(status);
      if(status === 'success') { //if not success, make another attempt
        page.render('abc'+i+'.png');
      }
      run(i-1);
    });
}
run(3);

这篇关于使用PhantomJS不会在循环中多次尝试呈现PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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