DEV Community

Eric The Coder
Eric The Coder

Posted on

Ruby: Get and save google images with Selenium

Hi, here a little Ruby code to get and save images from Google Image. Can be handy :-)

If you need help with selenium web driver: https://help.crossbrowsertesting.com/selenium-testing/tutorials/getting-started-with-ruby-and-selenium/

require 'selenium-webdriver'
require 'faraday'

def search_google(search_query)

  driver = Selenium::WebDriver.for :chrome
  search_url = "https://www.google.com/search?site=&tbm=isch&source=hp&biw=1873&bih=990&q=#{search_query}"
  images_url = []

  # Open the browser and navigate to Google search image
  #driver.navigate.to search_url
  driver.navigate.to search_url
  elements = driver.find_elements(class: 'rg_i')

  elements.each.with_index do |e, index|
    #Click on the image (to get the full size image)
    e.click
    sleep(1)

    #Get the full size image and put it's url in a array
    element = driver.find_elements(class: 'v4dQwb')

    #This is google site specific logic
    if index == 0
      big_img = element[0].find_element(class: 'n3VNCb')
    else
      big_img = element[1].find_element(class: 'n3VNCb')
    end
    images_url.push big_img.attribute("src")

    begin
      # Get images data 
      response = Faraday.get(images_url[index], {}, {})

      #Write image to search.jpg file
      File.open("search#{index+1}.jpg",'w') do |f|
            f.puts response.body
      end
    rescue => exception
      puts 'Error saving image'
    end  

    # will only read 5 images
    if index == 5
      break
    end

  end
end

search_google 'dog'
Enter fullscreen mode Exit fullscreen mode

Alt Text

Top comments (0)