Ruby · save_images · watir

How do I save all images on a webpage?

Answer:

Code:

require ‘watir’

browser = Watir::IE.new
browser.visible=true
browser.goto(‘http://in.youtube.com/’)

idx = 0

# using the images collection, iterate through all of the images on a page
browser.images.each do |x|
idx += 1
# apparently the string accepted by the string method will not allow variable substitution
original_type= x.src[-4,4]

type=”.gif” if original_type == ‘.gif’
type=”.jpg” if original_type == ‘.jpg’
type=”.png” if original_type == ‘.png’
type=”.bmp” if original_type == ‘.bmp’
type=”jpeg” if original_type == ‘.jpeg’

location = ‘C:\TML\fileq-‘ + idx.to_s + type
x.save(location)
end