Skip to content

Instantly share code, notes, and snippets.

@jakalada
Created December 15, 2009 17:59
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 jakalada/257127 to your computer and use it in GitHub Desktop.
Save jakalada/257127 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby19
# coding: utf-8
require 'mechanize'
hiragana_characters = %w(
あ い う え お
か き く け こ
さ し す せ そ
た ち つ て と
な に ぬ ね の
は ひ ふ へ ほ
ま み む め も
や ゆ よ
ら り る れ ろ
わ を
)
agent = WWW::Mechanize.new
agent.user_agent_alias = 'Linux Mozilla'
agent.get('http://www.google.co.jp/')
result_xpath = 'p#resultStats/b[2]/text()'
results = {}
agent.page.form_with(:name => 'f') do |form|
hiragana_characters.each do |ch|
form.field_with(:name => 'q').value = ch
form.click_button
result = agent.page.at(result_xpath)
results[ch] = result.to_s.delete(',').to_i
sleep 1
end
end
results = results.sort {|a, b| a[1] <=> b[1]}
results.each_index do |index|
results[index][1] = results[index][1].to_s.reverse.scan(/\d{1,3}/).join(",").reverse
end
greatest_width = results.last[1].length
format = "%#{greatest_width}s"
results.reverse.each do |ch, result|
print "#{ch} #{format % result}\n"
end
puts "\n"
puts Time.now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment