Ruby Web Search

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
require ‘net/http’
require ‘rexml/document’
# Web search for "finance"
url = ‘http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=cls31415o&query=finance&results=40'
# get the XML data as a string
xml_data = Net::HTTP.get_response(URI.parse(url)).body
# extract event information
doc = REXML::Document.new(xml_data)
titles = []
links = []
doc.elements.each(‘ResultSet/Result/Title’) do |ele|
   titles << ele.text
end
doc.elements.each(‘ResultSet/Result/Url’) do |ele|
   links << ele.text
end
# print all events
titles.each_with_index do |title, idx|
   print "#{title} => #{links[idx]}\n"
end

Comments are closed.