Skip to content

Instantly share code, notes, and snippets.

@ytnk531
Created January 2, 2021 04:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ytnk531/db0762d1d217edf6580c5f669285d53c to your computer and use it in GitHub Desktop.
Save ytnk531/db0762d1d217edf6580c5f669285d53c to your computer and use it in GitHub Desktop.
parse image on google search
require 'uri'
require 'open-uri'
require 'net/http'
require 'bundler/inline'
require 'securerandom'
gemfile do
source 'https://rubygems.org'
gem 'nokogiri'
end
def get(url)
uri = URI.parse url
resp = Net::HTTP.get_response(uri)
Nokogiri::HTML.parse(resp.body)
end
def download_image(image_url)
ext = image_url.split('.').last
filename = "#{SecureRandom.hex}.jpg"
URI.open(image_url) do |img|
File.open(filename, 'wb') do
_1.write(img.read)
end
end
end
o = get('https://www.google.com/search?q=effector+board&tbm=isch&ved=2ahUKEwjKwLnNoe_sAhVYet4KHcwmBgkQ2-cCegQIABAA&oq=effector+board&gs_lcp=CgNpbWcQAzIECCMQJzoKCAAQsQMQgwEQBDoHCAAQsQMQBDoECAAQBDoHCCMQ6gIQJzoCCAA6BQgAELEDOgYIABAFEB46BggAEAgQHjoECAAQHlCTjgZY7b8GYLbBBmgCcAB4AYABhAWIAZQYkgEMMS4xMS4xLjIuMC4xmAEAoAEBqgELZ3dzLXdpei1pbWewAQrAAQE&sclient=img&ei=yfWlX4q4MNj0-QbMzZhI&bih=274&biw=1034&hl=en')
o.css('img').map { _1.attributes['src'].value }
.filter { _1.include?('https://') }
.each { download_image _1 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment