Skip to content

Instantly share code, notes, and snippets.

@3on
Last active October 7, 2015 05:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3on/3112441 to your computer and use it in GitHub Desktop.
Save 3on/3112441 to your computer and use it in GitHub Desktop.
Scraping bootstrap for phantom.js
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;
}
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);
});
@albacoretuna
Copy link

Well written!
My problem is when I try to output an array like this: [[234.23,2342.12,"Hello"]] I always get the last string without quotation marks: [[234.23,2342.12,Hello]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment