I'm trying to get the images of a given webpage. I've tried using the modules request
and jsdom
as follows:
var request = require('request'),
jsdom = require('jsdom');
request({ uri:'http://www.google.com' }, function (error, response, body) {
if (error && response.statusCode !== 200) {
console.log('Error when contacting google.com')
}
jsdom.env({
html: body,
scripts: [
'http://code.jquery.com/jquery-1.8.2.min.js'
]
}, function (err, window) {
var $ = window.jQuery;
$(function () {
console.log($('img').length);
})
});
});
Unfortunately, console.log($('img').length);
prints 0
despite the page google.com
containing images...
What am I doing wrong?
You can use Cheerio jquery implementation in server.
Here is the Coffee script Gist to read image URL from the site
https://gist.github.com/mrjjwright/3240020
Javascript version