我正在尝试复制phantomJS netlog.js的功能,仅在nodeJS中。我正在用幻影-节点模块作为桥梁。
通常,这将在使用phantomjs netlog.js http://www.google.com/的命令行中无头运行。它将返回很多包含所有网络请求和响应的json。
我在这里所做的是尝试在使用netlog.js模块创建的页面中运行phantomjs-node代码(忽略netlog.js中的var page = require('webpage').create()行)。
虽然代码没有中断,但我没有得到json的返回。这里怎么了?我是否需要以某种方式传输页面请求?
在app.js中:
var phantom = require('phantom');
siteUrl = "http://www.google.com/"
phantom.create(function (ph) {
ph.createPage(function (page) {
var system = require('system'),
address;
page.open(siteUrl, function (status) {
// console.log("opened " + siteUrl +"\n",status+"\n");
page.evaluate(function () {
if (system.args.length === 1) {
console.log('Usage: netlog.js <some URL>');
phantom.exit(1);
} else {
console.log(system.args[1])
address = system.args[1];
page.onResourceRequested = function (req) {
console.log('requested: ' + JSON.stringify(req, undefined, 4));
};
page.onResourceReceived = function (res) {
console.log('received: ' + JSON.stringify(res, undefined, 4));
};
page.open(address, function (status) {
if (status !== 'success') {
console.log('FAIL to load the address');
}
phantom.exit();
});
}
}, function finished(result) {
ph.exit();
},thirdLayerLinks);
});
});
}, {
dnodeOpts: {
weak: false
}
});发布于 2014-10-10 17:16:04
你在复制粘贴时犯了个错误。不应该有一个page.evaluate调用和一个page.open调用。你从基本的幻影节点代码中学到了太多。
PhantomJS和Node.js有不同的运行时和非常不同的模块。没有phantom引用。此外,节点中没有system。你可能指的是process。
然后,文档会这样说:
不能直接设置回调,而是使用
page.set('callbackName', callback)
固定代码:
var phantom = require('phantom');
var address = "http://google.com/";
phantom.create(function (ph) {
ph.createPage(function (page) {
page.set("onResourceRequested", function (req) {
console.log('requested: ' + JSON.stringify(req, undefined, 4));
});
page.set("onResourceReceived", function (res) {
console.log('received: ' + JSON.stringify(res, undefined, 4));
});
page.open(address, function (status) {
if (status !== 'success') {
console.log('FAIL to load the address');
}
ph.exit();
});
});
}, {
dnodeOpts: {
weak: false
}
});https://stackoverflow.com/questions/26303494
复制相似问题