public
Last active

Scraping bootstrap for phantom.js

  • Download Gist
phantom.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
var system = require('system');
 
var page = require('webpage').create();
 
var url = "http://www.boston.com/bigpicture/";
 
var scrap = function(){
var r = {};
r.imgCount = $('img.bpImage').length;
r.imgURL = [];
$('img.bpImage').each(function(){
r.imgURL.push($(this).attr('src'));
});
return JSON.stringify(r);
};
 
page.open(url, function(status) {
if(status !== 'success') {
throw "Page load issue " + status;
return;
}
page.onError = function (msg, trace) {
console.error('error > ' + msg);
console.error(trace);
}
page.onAlert = function(msg) {
console.error('alert > ' + msg);
}
page.onConsoleMessage = function (msg, line, source) {
var l = (line != undefined) ? ' @ line: ' + line : '';
console.error('> ' + msg + l);
}
 
window.setTimeout(function(){
page.includeJs("https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js", function() {
var res = page.evaluate(scrap);
console.log(res);
 
phantom.exit();
})
}, 1000);
 
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.