将值从PhantomJS传递到node.js [英] Passing a value from PhantomJS to node.js

查看:75
本文介绍了将值从PhantomJS传递到node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个phantomJS脚本,可通过 exec() 调用node.js脚本. 现在,我需要从PhantomJS脚本返回一个字符串,以便可以在节点中重新使用它.
有办法实现吗?

I have a phantomJS script that is executed via an exec() call in a node.js script. Now I need to return a string from the PhantomJS script so that it can be utilized back in node.
Is there a way to achieve that ?

节点应用:

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中对其进行了解析.

Yeah just output a JSON string from PhantomJS using JSON.stringify(result) and parse it in node.js with JSON.parse(stdout).

例如:

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

这里有一些样板,介绍了如何使用PhantomJS进行抓取.

Here is some boilerplate for how to use PhantomJS to scrape.

这篇关于将值从PhantomJS传递到node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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