【发布时间】:2014-07-21 12:46:28
【问题描述】:
我正在使用 PhantomJS 通过npm-phantom 模块从基于 AJAX 的页面中抓取数据。有时当 phantom 开始 DOM 遍历时数据还没有加载。如何将window.onload = function() { ... } 之类的内容插入page.evaluate?它返回一个函数,但不返回数据。
var phantom = require('phantom');
exports.main = function (url, callback) {
phantom.create(function (ph) {
ph.createPage(function (page) {
page.open(pref + url, function (status) {
page.evaluate(function () {
// here
var data = {};
data.one = document.getElementById("first").innerText;
data.two = document.getElementById("last").innerText;
return data;
},
function (res) {
callback(null, res);
ph.exit();
});
});
});
});
}
在 PhantomJS API 页面上我找到了onLoadFinished,但它是如何应用的。
【问题讨论】: