Scraping google images [web harvesting] | quick way to get images from google

October 15, 2009

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.