How to resolve the algorithm Yahoo! search interface step by step in the Ruby programming language
How to resolve the algorithm Yahoo! search interface step by step in the Ruby programming language
Table of Contents
Problem Statement
Create a class for searching Yahoo! results. It must implement a Next Page method, and read URL, Title and Content from results.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Yahoo! search interface step by step in the Ruby programming language
The source code you provided is a Ruby program that searches for results on Yahoo! using the Hpricot library for parsing HTML.
The SearchResult
struct is defined to store the URL, title, and content of each search result.
The SearchYahoo
class is defined to perform the search. It takes a search term as a parameter and initializes the @term
, @page
, @results
, and @url
instance variables.
The next_result
method returns the next search result. If there are no more results, it calls the next_page
method to fetch the next page of results.
The fetch_results
method fetches the search results from Yahoo! using the @url
instance variable. It uses the open
method to open the URL and the Hpricot
library to parse the HTML.
The next_page
method increments the @page
instance variable by 10 and calls the fetch_results
method.
The parse_html
method parses the HTML document and extracts the search results. It uses the search
method to find the elements that contain the search results.
The main
method creates a new SearchYahoo
object with the search term "test". It then calls the next_result
method 15 times and prints the title, URL, and content of each search result.
Source code in the ruby programming language
require 'open-uri'
require 'hpricot'
SearchResult = Struct.new(:url, :title, :content)
class SearchYahoo
@@urlinfo = [nil, 'ca.search.yahoo.com', 80, '/search', nil, nil]
def initialize(term)
@term = term
@page = 1
@results = nil
@url = URI::HTTP.build(@@urlinfo)
end
def next_result
if not @results
@results = []
fetch_results
elsif @results.empty?
next_page
end
@results.shift
end
def fetch_results
@url.query = URI.escape("p=%s&b=%d" % [@term, @page])
doc = open(@url) { |f| Hpricot(f) }
parse_html(doc)
end
def next_page
@page += 10
fetch_results
end
def parse_html(doc)
doc.search("div#main").search("div").each do |div|
next unless div.has_attribute?("class") and div.get_attribute("class").index("res") == 0
result = SearchResult.new
div.search("a").each do |link|
next unless link.has_attribute?("class") and link.get_attribute("class") == "yschttl spt"
result.url = link.get_attribute("href")
result.title = link.inner_text
end
div.search("div").each do |abstract|
next unless abstract.has_attribute?("class") and abstract.get_attribute("class").index("abstr")
result.content = abstract.inner_text
end
@results << result
end
end
end
s = SearchYahoo.new("test")
15.times do |i|
result = s.next_result
puts i+1
puts result.title
puts result.url
puts result.content
puts
end
You may also check:How to resolve the algorithm Haversine formula step by step in the ooRexx programming language
You may also check:How to resolve the algorithm Sleep step by step in the PL/I programming language
You may also check:How to resolve the algorithm Long multiplication step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Reflection/List properties step by step in the zkl programming language
You may also check:How to resolve the algorithm File size step by step in the Aime programming language