从java调用时,Phantomjs会挂起

时间:2014-01-05 00:24:22

标签: java javascript web-scraping phantomjs

我正在尝试从Java调用phantomjs脚本,但不幸的是在下面的片段中挂起:

  var ua = page.evaluate(function () {
       return document.getElementsByTagName('html')[0].outerHTML;
   });

你有什么想法吗?其余部分如下:

loadspeed.js:

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

t = Date.now();
address = "http://query.nytimes.com/search/sitesearch/#/africa+floods";

page.open(address, function (status) {
    if (status !== 'success') {
        console.log('FAIL to load the address');
    } else {     
        var ua = page.evaluate(function () {
       return document.getElementsByTagName('html')[0].outerHTML;
   });
    console.log(ua);
    }
    phantom.exit();
});

爪哇:

public static void main(String[] args) throws IOException, InterruptedException, ScriptException {

    String command="cmd /c phantomjs loadspeed.js";

    Process process = Runtime.getRuntime().exec(command);
    int exitStatus =  process.waitFor();
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

    String currentLine=null;
    StringBuilder stringBuilder = new StringBuilder(exitStatus==0?"SUCCESS:":"ERROR:");
    currentLine= bufferedReader.readLine();
    while(currentLine !=null)
    {
        stringBuilder.append(currentLine);
        currentLine = bufferedReader.readLine();
    }
    System.out.println(stringBuilder.toString());

1 个答案:

答案 0 :(得分:0)

事实证明,Windows必须使用小缓冲区来处理此任务。更多内容: http://dhruba.name/2012/10/16/java-pitfall-how-to-prevent-runtime-getruntime-exec-from-hanging/