Skip to content

Instantly share code, notes, and snippets.

@noel
Created August 1, 2010 20:05
Show Gist options
  • Save noel/503709 to your computer and use it in GitHub Desktop.
Save noel/503709 to your computer and use it in GitHub Desktop.
# Download all images from a stock exchange lightbox
require 'rubygems'
require 'mechanize'
require 'logger'
agent = Mechanize.new { |a| a.log = Logger.new("mech.log") }
agent.user_agent_alias = 'Mac Safari'
page = agent.get("http://www.sxc.hu/")
#log into site
login_form = page.forms[2]
login_form['login'] = "MY_USERNAME"
login_form['pass'] = "MY_PASSWORD"
login_form.submit
# go to the lightboxes page
agent.page.link_with(:text => "Lightboxes").click
# choose the specific lightbox
agent.page.link_with(:text=>"Favourites").click
# Create an Output Directory
FileUtils.mkdir_p 'images'
# get the images
agent.page.search("#thumbs img").each do |image|
image_name = image.attributes['alt'] #this is the image number
image_url = "http://www.sxc.hu/browse.phtml?f=download&id=" + image_name
# load page with image
img_page = agent.get(image_url)
# find the image tag to get the true source of the image
agent.page.search("img").each do |img|
path = img.attributes['src']
# save the image
agent.get( path ).save_as "images/#{image_name}.jpg"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment