[英]Is it possible to run PhantomJS from node.js as a command line argument
[英]Passing a value from PhantomJS to node.js
我有一個phantomJS腳本,通過node.js腳本中的exec()
調用執行。 現在我需要從PhantomJS腳本返回一個字符串,以便可以在節點中使用它。
有沒有辦法實現這一目標?
節點應用:
child = exec('./phantomjs dumper.js',
function (error, stdout, stderr) {
console.log(stdout, stderr); // Always empty
});
dumper.js(幻影)
var system = require('system');
var page = require('webpage').create();
page.open( system.args[1], function (status) {
if (status !== 'success') {
console.log('Unable to access the network!');
} else {
return "String"; // Doesn't work
}
phantom.exit('String2'); //Doesn't work either
});
是的只是使用JSON.stringify(result)
從PhantomJS輸出JSON字符串,並使用JSON.parse(stdout)
在node.js中解析它。
像這樣例如:
Node.js的:
child = exec('./phantomjs dumper.js',
function (error, stdout, stderr) {
console.log(stdout, stderr); // Always empty
var result = JSON.parse(stdout);
}
);
PhantomJS:
var system = require('system');
var page = require('webpage').create();
page.open( system.args[1], function (status) {
if (status !== 'success') {
console.log('Unable to access the network!');
} else {
console.log(JSON.stringify({string:"This is a string", more: []}));
}
phantom.exit();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.