我有一个phantomJS脚本,通过一个node.js脚本中的一个
现在我需要从PhantomJS脚本返回一个字符串,以便可以在节点中使用.
有办法实现吗?
exec()
调用执行.
现在我需要从PhantomJS脚本返回一个字符串,以便可以在节点中使用.
有办法实现吗?
节点应用:
child = exec('./phantomjs dumper.js',function (error,stdout,stderr) { console.log(stdout,stderr); // Always empty });
dumper.js(Phantom)
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',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(); });
Here is some boilerplate如何使用PhantomJS刮.
原文地址:https://www.jb51.cc/js/151875.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。