Skip to content

Instantly share code, notes, and snippets.

@chrishamant
Created April 5, 2011 17:29
Show Gist options
  • Save chrishamant/904064 to your computer and use it in GitHub Desktop.
Save chrishamant/904064 to your computer and use it in GitHub Desktop.
use node to scrape and save some images from a url
#!/usr/bin/env coffee
sys = require 'sys'
fs = require 'fs'
http = require 'http'
tovisit = []
imgcount = 0
getImage= (url)->
options =
host: 'hostname.com'
port: 80
path: url
http.get(options, (res)->
size = parseInt(res.headers['content-length'],10)
buffer = new Buffer(size)
start = 0
res.on 'data', (chunk)->
chunk.copy(buffer,start,0,chunk.length)
start += chunk.length
res.on 'end', ()->
filename = url.match(/id=(\d+)/)[1] + '.jpg'
fs.writeFile(filename , buffer, (err) ->
console.log err if err
imgcount++
)
).on('error', (e)->
console.log("Got error: " + e.message)
)
tovisit.push "/path/to/img.jpg"
getImage(url) for url in tovisit
#console.log("wrote " + imgcount + " images")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment