Jump to content

Yahoo! search interface: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: syntax coloured, fixed to work with current output.)
m (syntax highlighting fixup automation)
Line 7:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program yahoosearch64.s */
Line 373:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 383:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program yahoosearch.s */
Line 735:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 744:
=={{header|AutoHotkey}}==
translated from python example
<syntaxhighlight lang="autohotkey">test:
<lang AutoHotkey>test:
yahooSearch("test", 1)
yahooSearch("test", 2)
Line 774:
url := regexreplace(url, "<.*?>")
return url
}</langsyntaxhighlight>
 
=={{header|C sharp}}==
Line 780:
E. g. all implementations for this task which regex for
"<a class=" fail by now, after Yahoo has changed its output format.
<langsyntaxhighlight lang="csharp">using System;
using System.Net;
using System.Text.RegularExpressions;
Line 893:
}
}
</syntaxhighlight>
</lang>
 
=={{header|D}}==
{{trans|C#}}
<langsyntaxhighlight lang="d">import std.stdio, std.exception, std.regex, std.algorithm, std.string,
std.net.curl;
 
Line 955:
void main() {
writefln("%(%s\n%)", "test".YahooSearch.results);
}</langsyntaxhighlight>
{{out|Output (shortened)}}
<pre>
Line 973:
 
=={{header|Gambas}}==
<langsyntaxhighlight lang="gambas">Public Sub Form_Open()
Dim hWebView As WebView
 
Line 984:
hWebView.URL = "https://www.yahoo.com"
 
End</langsyntaxhighlight>
'''[http://www.cogier.com/gambas/Yahoo!%20search%20interface.png Click here to see output (I have typed 'rosettacode' in the search box)]'''
 
Line 991:
 
The regular expression used below was figured out by studying the raw HTML and works fine as at 18th November, 2019.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,053:
fmt.Println(res)
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,105:
=={{header|GUISS}}==
 
<langsyntaxhighlight lang="guiss">Start,Programs,Applications,Mozilla Firefox,Inputbox:address bar>www.yahoo.co.uk,
Button:Go,Area:browser window,Inputbox:searchbox>elephants,Button:Search</langsyntaxhighlight>
 
=={{header|Haskell}}==
Haskell is not an object oriented language, so this example does not implement an object class.
However, it can be interesting as an example of how HTML source code can be parsed using the Parsec library.
<langsyntaxhighlight Haskelllang="haskell">import Network.HTTP
import Text.Parsec
 
Line 1,216:
in [ YahooSearchItem { itemUrl = u, itemTitle = t, itemContent = c } |
(u, t, c) <- zip3 us ts cs ]
</syntaxhighlight>
</lang>
Simple invocation from GHCi: <pre>yahoo "Rosetta%20code" >>= printResults</pre>. Notice that spaces must be expressed as "%20", because spaces are not allowed in URLs.
==Icon and {{header|Unicon}}==
The following uses the Unicon pre-processor and messaging extensions and won't run under Icon without significant modification.
The code provides a suitable demonstration; however, could be made more robust by things such as URL escaping the search string
<langsyntaxhighlight Iconlang="icon">link printf,strings
 
procedure main()
Line 1,263:
urlpat := sprintf("http://search.yahoo.com/search?p=%s&b=%%d",searchtext)
page := 0
end</langsyntaxhighlight>
 
{{libheader|Icon Programming Library}}
Line 1,304:
=={{header|Java}}==
{{incorrect|Java}}
<langsyntaxhighlight lang="java">import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
Line 1,460:
}
}
}</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">""" Rosetta Code Yahoo search task. https://rosettacode.org/wiki/Yahoo!_search_interface """
 
using EzXML
Line 1,541:
println("URL: ", res.url, "\n")
end
</langsyntaxhighlight>{{out}}
<pre>
Searching Yahoo for `RosettaCode`:
Line 1,707:
{{incorrect|Kotlin}}
This is based on the C# entry but uses a regular expression based on what appears to be the Yahoo! format as at the date of this entry (4 December 2017).
<langsyntaxhighlight lang="scala">// version 1.2.0
 
import java.net.URL
Line 1,751:
for (result in x.results.take(3)) println(result)
}
}</langsyntaxhighlight>
Output (restricted to first three results on first two pages):
<pre>PAGE 1 =>
Line 1,783:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
We cannot define a class in Mathematica, so I generate a "Manipulate" object instead.
<syntaxhighlight lang="text">Manipulate[
Column[Flatten[
StringCases[
Line 1,807:
Row[{Button["Search", page = 1; query = input],
Button["Prev", page -= 10, Enabled -> Dynamic[page >= 10]],
Button["Next", page += 10]}]]</langsyntaxhighlight>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import httpclient, strutils, htmlparser, xmltree, strtabs
const
PageSize = 7
Line 1,864:
searchInf.nextPage()
searchInf.echoResult()
</syntaxhighlight>
</lang>
{{out}}
<pre>(url: "https://weather.com/", title: "National and Local Weather Radar, Daily Forecast, Hurricane ...", content: "The Weather Channel and weather.com provide a national and local weather forecast for cities, as well as weather radar, report and hurricane coverage ")
Line 1,890:
 
We implement some simple parsing with logic programming. Regular expressions in Oz don't seem to support lazy quantification which makes parsing the result pages with them difficult.
<langsyntaxhighlight lang="oz">declare
[HTTPClient] = {Module.link ['x-ozlib://mesaros/net/HTTPClient.ozf']}
[StringX] = {Module.link ['x-oz://system/String.ozf']}
Line 2,036:
end
in
{ExampleUsage}</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">package YahooSearch;
 
use Encode;
Line 2,106:
$invocant->{link_to_next} = $next;
$invocant->{results} = $results;
return 1;}</langsyntaxhighlight>
 
=={{header|Phix}}==
Line 2,120:
{"\xC2\xAE","[R]"}}, -- registered
</pre>
<!--<langsyntaxhighlight Phixlang="phix">(notonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Yahoo_search_interface.exw
Line 2,198:
<span style="color: #0000FF;">?</span><span style="color: #008000;">"done"</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,239:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(load "@lib/http.l")
 
(de yahoo (Query Page)
Line 2,263:
(T (eof))
(T (= "</div" (till ">" T)))
(char) ) ) ) ) ) ) ) ) ) )</langsyntaxhighlight>
Output:
<pre>: (more (yahoo "test"))
Line 2,273:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">import urllib
import re
 
Line 2,317:
for result in x.search_results:
print result.title</langsyntaxhighlight>
 
=={{header|R}}==
Line 2,325:
 
Rather than using regexes to find the content (like some of the other solutions here) this method parses the HTML and finds the appropriate sections.
<langsyntaxhighlight Rlang="r">YahooSearch <- function(query, page=1, .opts=list(), ignoreMarkUpErrors=TRUE)
{
if(!require(RCurl) || !require(XML))
Line 2,414:
#Usage
YahooSearch("rosetta code")
nextpage()</langsyntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight Racketlang="racket">#lang racket
(require net/url)
(define *yaho-url* "http://search.yahoo.com/search?p=~a&b=~a")
Line 2,445:
(define (next-page)
(set! *current-page* (add1 *current-page*))
(search-yahoo *current-query*))</langsyntaxhighlight>
 
# REPL:
Line 2,523:
YahooSearch.rakumod:
 
<syntaxhighlight lang="raku" line>
<lang perl6>
use Gumbo;
use LWP::Simple;
Line 2,579:
YahooSearch.new($search-term).results.next.results;
}
</syntaxhighlight>
</lang>
 
And the invocation script is simply:
Line 2,590:
 
Should give out something like the following:
<syntaxhighlight lang="text">
=== #1 ===
Title:
Line 2,603:
URL: https://video.search.yahoo.com/search/video?p=test
Text: More Test videos
</syntaxhighlight>
</lang>
 
...and should go up to result #21!
Line 2,610:
{{improve|1=Ruby|2=Should not call <code>URI.escape</code>, because it fails to encode = signs and some other characters that might appear in the term. See [[URL encoding#Ruby]].}}
Uses {{libheader|RubyGems}} {{libheader|Hpricot}} to parse the HTML. Someone more skillful than I at XPath or CSS could tighten up the <code>parse_html</code> method.
<langsyntaxhighlight lang="ruby">require 'open-uri'
require 'hpricot'
 
Line 2,672:
puts result.content
puts
end</langsyntaxhighlight>
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">'--------------------------------------------------------------------------
' send this from the server to the clients browser
'--------------------------------------------------------------------------
Line 2,712:
[exit]
cls ' clear browser screen and get outta here
wait</langsyntaxhighlight>
This user input sits at the top of the yahoo page so they can select a new search or page
<div>[[File:yahooSearchRunBasic.png‎]]</div>
Line 2,719:
{{trans|Python}}
 
<langsyntaxhighlight lang="tcl">package require http
 
proc fix s {
Line 2,756:
foreach {title content url} [Nextpage] {
puts $title
}</langsyntaxhighlight>
{{works with|Tcl|8.6}}
With Tcl 8.6, more options are available for managing the global state, through objects and coroutines. First, an object-based solution that takes the basic <tt>YahooSearch</tt> functionality and dresses it up to be more Tcl-like:
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
oo::class create WebSearcher {
Line 2,792:
puts "\"$title\" : $url"
}
$ytest delete ;# standard method that deletes the object</langsyntaxhighlight>
However, the paradigm of an iterator is also interesting and is more appropriately supported through a coroutine. This version conceals the fact that the service produces output in pages; care should be taken with it because it can produce rather a lot of network traffic...
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
proc yahoo! term {
Line 2,816:
puts [dict get [$it] title]
after 300 ;# Slow the code down... :-)
}</langsyntaxhighlight>
 
Another approach: uses a class as specified in the task. Also, uses an html parser from tcllib (parsing html with regular expressions is a particular annoyance of [[User:Glennj|mine]]).
Line 2,823:
{{tcllib|htmlparse}}
{{tcllib|textutil::adjust}}<!-- for formatting output -->
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
package require http
package require htmlparse
Line 2,936:
puts ""
}
}</langsyntaxhighlight>
 
=={{header|TXR}}==
Line 2,946:
A little sprinkling of regex is used.
 
<langsyntaxhighlight lang="txr">#!/usr/bin/txr -f
@(next :args)
@(cases)
Line 2,968:
@ (end)
@(end)
</syntaxhighlight>
</lang>
 
Sample run:
Line 3,022:
 
A somewhat ''ad hoc'' solution as the output format for Yahoo! search seems to change quite frequently. All I can say is that this worked on 5th January, 2022.
<langsyntaxhighlight lang="ecmascript">/* yahoo_search_interface.wren */
 
import "./pattern" for Pattern
Line 3,107:
}
System.print("Displayed %(limit) pages with a total of %(totalCount) entries.")
curl.easyCleanup()</langsyntaxhighlight>
<br>
We now embed this script in the following C program, build and run.
<langsyntaxhighlight lang="c">/* gcc yahoo_search_interface.c -o yahoo_search_interface -lcurl -lwren -lm */
 
#include <stdio.h>
Line 3,300:
free(script);
return 0;
}</langsyntaxhighlight>
 
{{out}}
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.