Created
September 9, 2013 03:38
-
-
Save hengkiardo/6491236 to your computer and use it in GitHub Desktop.
Download images with node.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
// npm install cheerio | |
// npm install request | |
function getImages(uri) { | |
var request = require('request'); | |
var url = require('url'); | |
var cheerio = require('cheerio'); | |
path = require('path') | |
var fs = require('fs'); | |
request(uri, function (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
$ = cheerio.load(body) | |
imgs = $('img').toArray() | |
console.log("Downloading...") | |
imgs.forEach(function (img) { | |
//console.log(img.attribs.src) | |
process.stdout.write("."); | |
img_url = img.attribs.src | |
if (/^https?:\/\//.test(img_url)) { | |
img_name = path.basename(img_url) | |
request(img_url).pipe(fs.createWriteStream(img_name)) | |
} | |
}) | |
console.log("Done!") | |
} | |
}) | |
} | |
getImages("http://imgur.com/gallery") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment