Friday, 15 July 2011

httprequest - Extract all images from an external site [Node.js] -



httprequest - Extract all images from an external site [Node.js] -

i'm working in code takes images website, send images string browser, doesn't works!

i'm trying utilize http module create server, principal page of pinterest, match images tags, store each match in array , send it.

this code:

var http = require('http') , options = { host: 'www.pinterest.com' , port: 80 , path: '/' , method: 'get' } , images = [ ] ; http.createserver( function ( request, response ) { http.request( options, function ( res ) { res.setencoding( 'utf8' ); res.on( 'data', function ( chunk ) { matches.push( chunk.match(/<img[^>]+src="([^">]+)/g) ); }); }).on('error', function(e) { console.log('problem request: ' + e.message); }); response.writehead( 200, { 'content-type' : 'text/html' } ); response.end( images.tostring() ); }).listen(8888);

i don't have error in console, after 1 minute, console prints:

problem request: socket hang up

i think have problem regex. anyway, method bring data:

var http = require('http') , options = { host: 'pinterest.com' , port: 80 , path: '/' , method: 'get' } , images = [ ]; http.createserver( function ( request, response ) { var req = http.get(options, function(res){ res.setencoding('utf8'); res.on('data', function (chunk) { images.push( chunk.match(/<img[^>]+src="([^">]+)/g) ); }).on('end', function(){ response.writehead( 200, { 'content-type' : 'text/javascript' } ); response.end(images.tostring()); }); }); req.on('error', function(error){ console.log('error: ' + error.message); response.writehead( 200, { 'content-type' : 'text/html' } ); response.end('error: ' + error.message); }); }).listen(8888);

i used here http.get method instead of http.request

node.js httprequest

No comments:

Post a Comment