How to resolve the algorithm Yahoo! search interface step by step in the Julia programming language
How to resolve the algorithm Yahoo! search interface step by step in the Julia 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 Julia programming language
This Julia code implements a Yahoo search interface. It defines a YahooSearch
struct that encapsulates the search query, the Yahoo URL to use, the current page being searched, a list of already used pages, and a list of search results. The search
function performs the actual search by making a GET request to the Yahoo URL with the specified query and page number. It parses the HTML response using the EzXML library and extracts the titles, contents, and URLs of the search results. The NextPage
function is used to navigate to the next page of search results.
The main function creates a YahooSearch
struct with the search query "RosettaCode". It then calls the search
function to perform the search and prints the titles, contents, and URLs of the search results.
Here's a step-by-step explanation of the code:
- The
PageSize
constant specifies the number of results to return per page. - The
URI
constant specifies the base URL for the Yahoo search API. - The
SearchResults
struct defines the structure of a search result, containing the title, content, and URL of the result. - The
YahooSearch
struct encapsulates the search query, the Yahoo URL, the current page being searched, a list of already used pages, and a list of search results. - The
NextPage
function is used to navigate to the next page of search results. - The
search
function performs the actual search by making a GET request to the Yahoo URL with the specified query and page number. It parses the HTML response using the EzXML library and extracts the titles, contents, and URLs of the search results. - The main function creates a
YahooSearch
struct with the search query "RosettaCode". It then calls thesearch
function to perform the search and prints the titles, contents, and URLs of the search results.
Source code in the julia programming language
""" Rosetta Code Yahoo search task. https://rosettacode.org/wiki/Yahoo!_search_interface """
using EzXML
using HTTP
using Logging
const pagesize = 7
const URI = "https://search.yahoo.com/search?fr=opensearch&pz=$pagesize&"
struct SearchResults
title::String
content::String
url::String
end
mutable struct YahooSearch
search::String
yahoourl::String
currentpage::Int
usedpages::Vector{Int}
results::Vector{SearchResults}
end
YahooSearch(s, url = URI) = YahooSearch(s, url, 1, Int[], SearchResults[])
function NextPage(yah::YahooSearch, link, pagenum)
oldpage = yah.currentpage
yah.currentpage = pagenum
search(yah)
yah.currentpage = oldpage
end
function search(yah::YahooSearch)
push!(yah.usedpages, yah.currentpage)
queryurl = yah.yahoourl * "b=$(yah.currentpage)&p=" * HTTP.escapeuri(yah.search)
req = HTTP.request("GET", queryurl)
# Yahoo's HTML is nonstandard, so send excess warnings from the parser to NullLogger
html = with_logger(NullLogger()) do
parsehtml(String(req.body))
end
for div in findall("//li/div", html)
if haskey(div, "class")
if startswith(div["class"], "dd algo") &&
(a = findfirst("div/h3/a", div)) != nothing &&
haskey(a, "href")
url, title, content = a["href"], nodecontent(a), "None"
for span in findall("div/p/span", div)
if haskey(span, "class") && occursin("fc-falcon", span["class"])
content = nodecontent(span)
end
end
push!(yah.results, SearchResults(title, content, url))
elseif startswith(div["class"], "dd pagination")
for a in findall("div/div/a", div)
if haskey(a, "href")
lnk, n = a["href"], tryparse(Int, nodecontent(a))
!isnothing(n) && !(n in yah.usedpages) && NextPage(yah, lnk, n)
end
end
end
end
end
end
ysearch = YahooSearch("RosettaCode")
search(ysearch)
println("Searching Yahoo for `RosettaCode`:")
println(
"Found ",
length(ysearch.results),
" entries on ",
length(ysearch.usedpages),
" pages.\n",
)
for res in ysearch.results
println("Title: ", res.title)
println("Content: ", res.content)
println("URL: ", res.url, "\n")
end
You may also check:How to resolve the algorithm Program termination step by step in the Groovy programming language
You may also check:How to resolve the algorithm Aliquot sequence classifications step by step in the zkl programming language
You may also check:How to resolve the algorithm Plasma effect step by step in the Perl programming language
You may also check:How to resolve the algorithm Documentation step by step in the PHP programming language
You may also check:How to resolve the algorithm Mouse position step by step in the M2000 Interpreter programming language