【问题标题】:Phantomjs hangs when called from java从 java 调用时,Phantomjs 挂起
【发布时间】:2014-01-05 00:24:22
【问题描述】:

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

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

你有什么想法吗?剩下的就在这里:

加载速度.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();
});

Java:

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());

【问题讨论】:

    标签: java javascript web-scraping phantomjs


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2013-05-15
      • 2016-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 2021-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多