gpt4 book ai didi

node.js - 将值从 PhantomJS 传递给 node.js

转载 作者:IT老高 更新时间:2023-10-28 23:20:34 24 4
gpt4 key购买 nike

我有一个通过 exec() 执行的 phantomJS 脚本调用 node.js 脚本。现在我需要从 PhantomJS 脚本中返回一个字符串,以便可以在 Node 中使用它。
有没有办法做到这一点?

Node 应用:

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

Here is some boilerplate了解如何使用 PhantomJS 进行抓取。

关于node.js - 将值从 PhantomJS 传递给 node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12979723/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com