Created
August 1, 2010 20:05
-
-
Save noel/503709 to your computer and use it in GitHub Desktop.
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
# 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