Skip to content

Instantly share code, notes, and snippets.

@sanryuu
Last active October 6, 2017 06:04
Show Gist options
  • Save sanryuu/c5e0081d5cd6a80d619c to your computer and use it in GitHub Desktop.
Save sanryuu/c5e0081d5cd6a80d619c to your computer and use it in GitHub Desktop.
CapybaraとSeleniumを使ってGoogle検索するサンプルコード ref: http://qiita.com/sanryuu/items/5b1b4794f5fd3b26679c
#! /usr/bin/env ruby
require "capybara"
require "selenium-webdriver"
Capybara.current_driver = :selenium
module Crowler
class Google
include Capybara::DSL
def hit_num keyword
visit URI.escape("https://www.google.co.jp/search?q=" + keyword)
result_status = all("#resultStats")[0]
unless result_status.nil?
result_status.text.match(/(\d+,)*\d+/)[0].gsub(",","").to_i
else
0
end
end
end
end
blowser = Crowler::Google.new
print blowser.hit_num "Capybara Selenium"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment