How to resolve the algorithm Yahoo! search interface step by step in the Go programming language
How to resolve the algorithm Yahoo! search interface step by step in the Go 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 Go programming language
This code shows a Yahoo search implementation in Go.
A YahooSearch object is defined, taking a query string (the search to make) and a page (the page of the results to get). The method results() returns a slice of YahooResults, each YahooResult being a struct with the title, the URL and the content of a search result. The method nextPage() returns a new YahooSearch object with the next page of results.
In the main function, we create a YahooSearch object with the query "rosettacode" and page 0 (to get the first page of results). Next, we print the first 5 results of the first page and then the first 5 results of the next page (page 2).
Now we explain the details:
-
We begin by defining a regular expression which we will use to extract the title, URL and content of each search result. The expression looks for an h3 element with a class attribute of "title" and an a element with a class attribute of ".?" (any class). Inside the a element, we capture the href attribute (the URL) and the text content (the title). After the h3 element, we look for a div element with a class attribute of "compText aAbs" and a p element with a class attribute of ".?". Inside the p element, we capture the text content (the content).
-
The results() method takes a YahooSearch object and returns a slice of YahooResults. It makes a GET request to the Yahoo search URL, reads the body of the response and parses it as a string. It then uses the regular expression to find all the search results on the page and creates a YahooResult for each one.
-
The nextPage() method takes a YahooSearch object and returns a new YahooSearch object with the next page of results. It increments the page number by 1.
-
In the main function, we create a YahooSearch object with the query "rosettacode" and page 0. We then print the first 5 results of the first page and the first 5 results of the next page (page 2).
Source code in the go programming language
package main
import (
"fmt"
"golang.org/x/net/html"
"io/ioutil"
"net/http"
"regexp"
"strings"
)
var (
expr = `<h3 class="title"><a class=.*?href="(.*?)".*?>(.*?)</a></h3>` +
`.*?<div class="compText aAbs" ><p class=.*?>(.*?)</p></div>`
rx = regexp.MustCompile(expr)
)
type YahooResult struct {
title, url, content string
}
func (yr YahooResult) String() string {
return fmt.Sprintf("Title : %s\nUrl : %s\nContent: %s\n", yr.title, yr.url, yr.content)
}
type YahooSearch struct {
query string
page int
}
func (ys YahooSearch) results() []YahooResult {
search := fmt.Sprintf("http://search.yahoo.com/search?p=%s&b=%d", ys.query, ys.page*10+1)
resp, _ := http.Get(search)
body, _ := ioutil.ReadAll(resp.Body)
s := string(body)
defer resp.Body.Close()
var results []YahooResult
for _, f := range rx.FindAllStringSubmatch(s, -1) {
yr := YahooResult{}
yr.title = html.UnescapeString(strings.ReplaceAll(strings.ReplaceAll(f[2], "<b>", ""), "</b>", ""))
yr.url = f[1]
yr.content = html.UnescapeString(strings.ReplaceAll(strings.ReplaceAll(f[3], "<b>", ""), "</b>", ""))
results = append(results, yr)
}
return results
}
func (ys YahooSearch) nextPage() YahooSearch {
return YahooSearch{ys.query, ys.page + 1}
}
func main() {
ys := YahooSearch{"rosettacode", 0}
// Limit output to first 5 entries, say, from pages 1 and 2.
fmt.Println("PAGE 1 =>\n")
for _, res := range ys.results()[0:5] {
fmt.Println(res)
}
fmt.Println("PAGE 2 =>\n")
for _, res := range ys.nextPage().results()[0:5] {
fmt.Println(res)
}
}
You may also check:How to resolve the algorithm Population count step by step in the 8086 Assembly programming language
You may also check:How to resolve the algorithm GUI component interaction step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Sorting algorithms/Comb sort step by step in the Groovy programming language
You may also check:How to resolve the algorithm One of n lines in a file step by step in the Swift programming language
You may also check:How to resolve the algorithm Deming's funnel step by step in the 11l programming language