yahoo検索で検索件数の取得

yahoo検索で検索件数の取得

#encoding: utf-8
require 'rubygems'
require 'hpricot'
require 'mechanize'
require 'open-uri'
require 'uri'
#$KCODE='u'


text1=URI.encode(ARGV[0]);

agent=Mechanize.new
agent.get("http://search.yahoo.co.jp/search?p=#{text1}&search.x=1&fr=top_ga1_sa&tid=top_ga1_sa&ei=UTF-8&aq=&oq=")
#q=Hpricot(open("http://yahoo.co.jp/search?q=test"))
q=Hpricot(agent.page.body)

text=(q/"#inf").inner_text.gsub(/.*/,"").gsub(/.*$/,"").gsub(",","").to_i;
puts text