Class: GoogleImageScrape

Inherits:
Object
  • Object
show all
Defined in:
lib/google_image_scrape.rb

Class Method Summary collapse

Class Method Details

.scrape_images(source_file) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/google_image_scrape.rb', line 6

def scrape_images(source_file)

  html_content = File.read(source_file)
  doc = Nokogiri::HTML(html_content)

  artworks = []

  doc.css('[class^="klitem"]').each do |artwork|
    name_element = artwork['aria-label']
    name = name_element.strip if name_element
    
    extensions_element = artwork.at_css('[class^="ellip"]')
    extensions = extensions_element.text.strip.split(',') if extensions_element

    link = artwork['href']
    image_element = artwork.at_css('[class^="rISBZc"]')
    image = image_element['src']

    artworks << {
      name: name,
      extensions: extensions,
      link: "https://www.google.com" + link.to_s,
      image: image
    }
  end
  
  output = { artworks: artworks }
  output_file_path = './results.json'
  File.write(output_file_path, JSON.pretty_generate(output))

  puts "Results saved to file: #{output_file_path}."
end