The code is pretty much self-explanatory. If you have questions don’t hesitate to write them.
require 'rubygems'
require 'mechanize'
agent = WWW::Mechanize.new
page = agent.get 'http://images.google.com/'
form = page.forms[0]
form['q']='obama' #type here what you wanna get
page = agent.submit form
uri = page.links[18].uri.to_s #gets the url
uri = uri[15..uri.index('&')-1]
agent.get(uri).save_as((File.basename(uri)).split("?")[0]) # saves the picture to script's directory
puts 'done'
Advertisement