I'm scraping data from AJAX-based pages using PhantomJS through the npm-phantom
module. Sometimes the data isn't loaded yet when phantom starts DOM traversal. How to insert something like window.onload = function() { ... }
into the page.evaluate
? It returns me a function, but not the data.
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();
});
});
});
});
}
On the PhantomJS API page I found onLoadFinished, but how does it apply.