find a button using mechanize

L

Li Chen

Hi Martin and Lex,

Thank you very much for all the help.

Here is final script which help me to
1) query the website
2) download the wav file
3) 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("http://www.ask.com/web?qsrc=2352&o=0&l=dir&dm=&q=dictionary")

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top