Last active
October 7, 2015 05:27
-
-
Save 3on/3112441 to your computer and use it in GitHub Desktop.
Scraping bootstrap for phantom.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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]]