Hi Martin and Lex,
Thank you very much for all the help.
Here is final script which help me to
- query the website
- download the wav file
- extract the definition using Hpricot.
Li
####################################
download wav file
extract the definition
require “rubygems”
require “mechanize”
mech = WWW::Mechanize.new
mech.user_agent_alias = “Windows IE 6”
mech.get(“dictionary, Ask.com”)
form = nil
form = mech.page.forms.select {|form| form.has_field?(“aj_text1”)}.first
if(form == nil)
abort “could not find form”
end
query the website of word “filly”
form.aj_text1 = “filly”
new_page = mech.submit(form)
download the wav file
wav_link = mech.page.links.select {|link| link.href =~/.wav$/i}.first
puts “downloading #{wav_link.href}”
File.open(File.basename(wav_link.href),‘w’){|f|
f.puts(mech.get_file(wav_link.href))}
extract the information
doc=Hpricot(new_page.body)
puts
doc.search(‘div/span/table/tr/td’).inner_html.gsub!(/</?[^>]*>/,“”)
###screen output
ruby mechanize7.rb
downloading http://media.ask.com/hm/ahd/pr/F0120600.wav
filly(n.)A young female horse.filly(n.)A lively, high-spirited girl or
young woman.
Exit code: 0