Yahoo! search interface: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: syntax coloured, fixed to work with current output.)
m (syntax highlighting fixup automation)
Line 7: Line 7:
=={{header|AArch64 Assembly}}==
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program yahoosearch64.s */
/* program yahoosearch64.s */
Line 373: Line 373:
/* for this file see task include a file in language AArch64 assembly */
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
{{Output}}
{{Output}}
<pre>
<pre>
Line 383: Line 383:
=={{header|ARM Assembly}}==
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* ARM assembly Raspberry PI */
/* program yahoosearch.s */
/* program yahoosearch.s */
Line 735: Line 735:
/***************************************************/
/***************************************************/
.include "../affichage.inc"
.include "../affichage.inc"
</syntaxhighlight>
</lang>
{{Output}}
{{Output}}
<pre>
<pre>
Line 744: Line 744:
=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
translated from python example
translated from python example
<syntaxhighlight lang="autohotkey">test:
<lang AutoHotkey>test:
yahooSearch("test", 1)
yahooSearch("test", 1)
yahooSearch("test", 2)
yahooSearch("test", 2)
Line 774: Line 774:
url := regexreplace(url, "<.*?>")
url := regexreplace(url, "<.*?>")
return url
return url
}</lang>
}</syntaxhighlight>


=={{header|C sharp}}==
=={{header|C sharp}}==
Line 780: Line 780:
E. g. all implementations for this task which regex for
E. g. all implementations for this task which regex for
"<a class=" fail by now, after Yahoo has changed its output format.
"<a class=" fail by now, after Yahoo has changed its output format.
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Net;
using System.Net;
using System.Text.RegularExpressions;
using System.Text.RegularExpressions;
Line 893: Line 893:
}
}
}
}
</syntaxhighlight>
</lang>


=={{header|D}}==
=={{header|D}}==
{{trans|C#}}
{{trans|C#}}
<lang d>import std.stdio, std.exception, std.regex, std.algorithm, std.string,
<syntaxhighlight lang="d">import std.stdio, std.exception, std.regex, std.algorithm, std.string,
std.net.curl;
std.net.curl;


Line 955: Line 955:
void main() {
void main() {
writefln("%(%s\n%)", "test".YahooSearch.results);
writefln("%(%s\n%)", "test".YahooSearch.results);
}</lang>
}</syntaxhighlight>
{{out|Output (shortened)}}
{{out|Output (shortened)}}
<pre>
<pre>
Line 973: Line 973:


=={{header|Gambas}}==
=={{header|Gambas}}==
<lang gambas>Public Sub Form_Open()
<syntaxhighlight lang="gambas">Public Sub Form_Open()
Dim hWebView As WebView
Dim hWebView As WebView


Line 984: Line 984:
hWebView.URL = "https://www.yahoo.com"
hWebView.URL = "https://www.yahoo.com"


End</lang>
End</syntaxhighlight>
'''[http://www.cogier.com/gambas/Yahoo!%20search%20interface.png Click here to see output (I have typed 'rosettacode' in the search box)]'''
'''[http://www.cogier.com/gambas/Yahoo!%20search%20interface.png Click here to see output (I have typed 'rosettacode' in the search box)]'''


Line 991: Line 991:


The regular expression used below was figured out by studying the raw HTML and works fine as at 18th November, 2019.
The regular expression used below was figured out by studying the raw HTML and works fine as at 18th November, 2019.
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 1,053: Line 1,053:
fmt.Println(res)
fmt.Println(res)
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,105: Line 1,105:
=={{header|GUISS}}==
=={{header|GUISS}}==


<lang guiss>Start,Programs,Applications,Mozilla Firefox,Inputbox:address bar>www.yahoo.co.uk,
<syntaxhighlight lang="guiss">Start,Programs,Applications,Mozilla Firefox,Inputbox:address bar>www.yahoo.co.uk,
Button:Go,Area:browser window,Inputbox:searchbox>elephants,Button:Search</lang>
Button:Go,Area:browser window,Inputbox:searchbox>elephants,Button:Search</syntaxhighlight>


=={{header|Haskell}}==
=={{header|Haskell}}==
Haskell is not an object oriented language, so this example does not implement an object class.
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.
However, it can be interesting as an example of how HTML source code can be parsed using the Parsec library.
<lang Haskell>import Network.HTTP
<syntaxhighlight lang="haskell">import Network.HTTP
import Text.Parsec
import Text.Parsec


Line 1,216: Line 1,216:
in [ YahooSearchItem { itemUrl = u, itemTitle = t, itemContent = c } |
in [ YahooSearchItem { itemUrl = u, itemTitle = t, itemContent = c } |
(u, t, c) <- zip3 us ts cs ]
(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.
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}}==
==Icon and {{header|Unicon}}==
The following uses the Unicon pre-processor and messaging extensions and won't run under Icon without significant modification.
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
The code provides a suitable demonstration; however, could be made more robust by things such as URL escaping the search string
<lang Icon>link printf,strings
<syntaxhighlight lang="icon">link printf,strings


procedure main()
procedure main()
Line 1,263: Line 1,263:
urlpat := sprintf("http://search.yahoo.com/search?p=%s&b=%%d",searchtext)
urlpat := sprintf("http://search.yahoo.com/search?p=%s&b=%%d",searchtext)
page := 0
page := 0
end</lang>
end</syntaxhighlight>


{{libheader|Icon Programming Library}}
{{libheader|Icon Programming Library}}
Line 1,304: Line 1,304:
=={{header|Java}}==
=={{header|Java}}==
{{incorrect|Java}}
{{incorrect|Java}}
<lang java>import java.io.BufferedReader;
<syntaxhighlight lang="java">import java.io.BufferedReader;
import java.io.IOException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InputStreamReader;
Line 1,460: Line 1,460:
}
}
}
}
}</lang>
}</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>""" Rosetta Code Yahoo search task. https://rosettacode.org/wiki/Yahoo!_search_interface """
<syntaxhighlight lang="julia">""" Rosetta Code Yahoo search task. https://rosettacode.org/wiki/Yahoo!_search_interface """


using EzXML
using EzXML
Line 1,541: Line 1,541:
println("URL: ", res.url, "\n")
println("URL: ", res.url, "\n")
end
end
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Searching Yahoo for `RosettaCode`:
Searching Yahoo for `RosettaCode`:
Line 1,707: Line 1,707:
{{incorrect|Kotlin}}
{{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).
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).
<lang scala>// version 1.2.0
<syntaxhighlight lang="scala">// version 1.2.0


import java.net.URL
import java.net.URL
Line 1,751: Line 1,751:
for (result in x.results.take(3)) println(result)
for (result in x.results.take(3)) println(result)
}
}
}</lang>
}</syntaxhighlight>
Output (restricted to first three results on first two pages):
Output (restricted to first three results on first two pages):
<pre>PAGE 1 =>
<pre>PAGE 1 =>
Line 1,783: Line 1,783:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
We cannot define a class in Mathematica, so I generate a "Manipulate" object instead.
We cannot define a class in Mathematica, so I generate a "Manipulate" object instead.
<lang>Manipulate[
<syntaxhighlight lang="text">Manipulate[
Column[Flatten[
Column[Flatten[
StringCases[
StringCases[
Line 1,807: Line 1,807:
Row[{Button["Search", page = 1; query = input],
Row[{Button["Search", page = 1; query = input],
Button["Prev", page -= 10, Enabled -> Dynamic[page >= 10]],
Button["Prev", page -= 10, Enabled -> Dynamic[page >= 10]],
Button["Next", page += 10]}]]</lang>
Button["Next", page += 10]}]]</syntaxhighlight>


=={{header|Nim}}==
=={{header|Nim}}==
<lang nim>import httpclient, strutils, htmlparser, xmltree, strtabs
<syntaxhighlight lang="nim">import httpclient, strutils, htmlparser, xmltree, strtabs
const
const
PageSize = 7
PageSize = 7
Line 1,864: Line 1,864:
searchInf.nextPage()
searchInf.nextPage()
searchInf.echoResult()
searchInf.echoResult()
</syntaxhighlight>
</lang>
{{out}}
{{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 ")
<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: 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.
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.
<lang oz>declare
<syntaxhighlight lang="oz">declare
[HTTPClient] = {Module.link ['x-ozlib://mesaros/net/HTTPClient.ozf']}
[HTTPClient] = {Module.link ['x-ozlib://mesaros/net/HTTPClient.ozf']}
[StringX] = {Module.link ['x-oz://system/String.ozf']}
[StringX] = {Module.link ['x-oz://system/String.ozf']}
Line 2,036: Line 2,036:
end
end
in
in
{ExampleUsage}</lang>
{ExampleUsage}</syntaxhighlight>


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>package YahooSearch;
<syntaxhighlight lang="perl">package YahooSearch;


use Encode;
use Encode;
Line 2,106: Line 2,106:
$invocant->{link_to_next} = $next;
$invocant->{link_to_next} = $next;
$invocant->{results} = $results;
$invocant->{results} = $results;
return 1;}</lang>
return 1;}</syntaxhighlight>


=={{header|Phix}}==
=={{header|Phix}}==
Line 2,120: Line 2,120:
{"\xC2\xAE","[R]"}}, -- registered
{"\xC2\xAE","[R]"}}, -- registered
</pre>
</pre>
<!--<lang Phix>(notonline)-->
<!--<syntaxhighlight lang="phix">(notonline)-->
<span style="color: #000080;font-style:italic;">--
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Yahoo_search_interface.exw
-- demo\rosetta\Yahoo_search_interface.exw
Line 2,198: Line 2,198:
<span style="color: #0000FF;">?</span><span style="color: #008000;">"done"</span>
<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>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 2,239: Line 2,239:


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(load "@lib/http.l")
<syntaxhighlight lang="picolisp">(load "@lib/http.l")


(de yahoo (Query Page)
(de yahoo (Query Page)
Line 2,263: Line 2,263:
(T (eof))
(T (eof))
(T (= "</div" (till ">" T)))
(T (= "</div" (till ">" T)))
(char) ) ) ) ) ) ) ) ) ) )</lang>
(char) ) ) ) ) ) ) ) ) ) )</syntaxhighlight>
Output:
Output:
<pre>: (more (yahoo "test"))
<pre>: (more (yahoo "test"))
Line 2,273: Line 2,273:


=={{header|Python}}==
=={{header|Python}}==
<lang python>import urllib
<syntaxhighlight lang="python">import urllib
import re
import re


Line 2,317: Line 2,317:
for result in x.search_results:
for result in x.search_results:
print result.title</lang>
print result.title</syntaxhighlight>


=={{header|R}}==
=={{header|R}}==
Line 2,325: 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.
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.
<lang R>YahooSearch <- function(query, page=1, .opts=list(), ignoreMarkUpErrors=TRUE)
<syntaxhighlight lang="r">YahooSearch <- function(query, page=1, .opts=list(), ignoreMarkUpErrors=TRUE)
{
{
if(!require(RCurl) || !require(XML))
if(!require(RCurl) || !require(XML))
Line 2,414: Line 2,414:
#Usage
#Usage
YahooSearch("rosetta code")
YahooSearch("rosetta code")
nextpage()</lang>
nextpage()</syntaxhighlight>


=={{header|Racket}}==
=={{header|Racket}}==
<lang Racket>#lang racket
<syntaxhighlight lang="racket">#lang racket
(require net/url)
(require net/url)
(define *yaho-url* "http://search.yahoo.com/search?p=~a&b=~a")
(define *yaho-url* "http://search.yahoo.com/search?p=~a&b=~a")
Line 2,445: Line 2,445:
(define (next-page)
(define (next-page)
(set! *current-page* (add1 *current-page*))
(set! *current-page* (add1 *current-page*))
(search-yahoo *current-query*))</lang>
(search-yahoo *current-query*))</syntaxhighlight>


# REPL:
# REPL:
Line 2,523: Line 2,523:
YahooSearch.rakumod:
YahooSearch.rakumod:


<syntaxhighlight lang="raku" line>
<lang perl6>
use Gumbo;
use Gumbo;
use LWP::Simple;
use LWP::Simple;
Line 2,579: Line 2,579:
YahooSearch.new($search-term).results.next.results;
YahooSearch.new($search-term).results.next.results;
}
}
</syntaxhighlight>
</lang>


And the invocation script is simply:
And the invocation script is simply:
Line 2,590: Line 2,590:


Should give out something like the following:
Should give out something like the following:
<lang>
<syntaxhighlight lang="text">
=== #1 ===
=== #1 ===
Title:
Title:
Line 2,603: Line 2,603:
URL: https://video.search.yahoo.com/search/video?p=test
URL: https://video.search.yahoo.com/search/video?p=test
Text: More Test videos
Text: More Test videos
</syntaxhighlight>
</lang>


...and should go up to result #21!
...and should go up to result #21!
Line 2,610: 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]].}}
{{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.
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.
<lang ruby>require 'open-uri'
<syntaxhighlight lang="ruby">require 'open-uri'
require 'hpricot'
require 'hpricot'


Line 2,672: Line 2,672:
puts result.content
puts result.content
puts
puts
end</lang>
end</syntaxhighlight>


=={{header|Run BASIC}}==
=={{header|Run BASIC}}==
<lang runbasic>'--------------------------------------------------------------------------
<syntaxhighlight lang="runbasic">'--------------------------------------------------------------------------
' send this from the server to the clients browser
' send this from the server to the clients browser
'--------------------------------------------------------------------------
'--------------------------------------------------------------------------
Line 2,712: Line 2,712:
[exit]
[exit]
cls ' clear browser screen and get outta here
cls ' clear browser screen and get outta here
wait</lang>
wait</syntaxhighlight>
This user input sits at the top of the yahoo page so they can select a new search or page
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>
<div>[[File:yahooSearchRunBasic.png‎]]</div>
Line 2,719: Line 2,719:
{{trans|Python}}
{{trans|Python}}


<lang tcl>package require http
<syntaxhighlight lang="tcl">package require http


proc fix s {
proc fix s {
Line 2,756: Line 2,756:
foreach {title content url} [Nextpage] {
foreach {title content url} [Nextpage] {
puts $title
puts $title
}</lang>
}</syntaxhighlight>
{{works with|Tcl|8.6}}
{{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:
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:
<lang tcl>package require Tcl 8.6
<syntaxhighlight lang="tcl">package require Tcl 8.6


oo::class create WebSearcher {
oo::class create WebSearcher {
Line 2,792: Line 2,792:
puts "\"$title\" : $url"
puts "\"$title\" : $url"
}
}
$ytest delete ;# standard method that deletes the object</lang>
$ytest delete ;# standard method that deletes the object</syntaxhighlight>
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...
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...
<lang tcl>package require Tcl 8.6
<syntaxhighlight lang="tcl">package require Tcl 8.6


proc yahoo! term {
proc yahoo! term {
Line 2,816: Line 2,816:
puts [dict get [$it] title]
puts [dict get [$it] title]
after 300 ;# Slow the code down... :-)
after 300 ;# Slow the code down... :-)
}</lang>
}</syntaxhighlight>


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]]).
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: Line 2,823:
{{tcllib|htmlparse}}
{{tcllib|htmlparse}}
{{tcllib|textutil::adjust}}<!-- for formatting output -->
{{tcllib|textutil::adjust}}<!-- for formatting output -->
<lang tcl>package require Tcl 8.6
<syntaxhighlight lang="tcl">package require Tcl 8.6
package require http
package require http
package require htmlparse
package require htmlparse
Line 2,936: Line 2,936:
puts ""
puts ""
}
}
}</lang>
}</syntaxhighlight>


=={{header|TXR}}==
=={{header|TXR}}==
Line 2,946: Line 2,946:
A little sprinkling of regex is used.
A little sprinkling of regex is used.


<lang txr>#!/usr/bin/txr -f
<syntaxhighlight lang="txr">#!/usr/bin/txr -f
@(next :args)
@(next :args)
@(cases)
@(cases)
Line 2,968: Line 2,968:
@ (end)
@ (end)
@(end)
@(end)
</syntaxhighlight>
</lang>


Sample run:
Sample run:
Line 3,022: 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.
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.
<lang ecmascript>/* yahoo_search_interface.wren */
<syntaxhighlight lang="ecmascript">/* yahoo_search_interface.wren */


import "./pattern" for Pattern
import "./pattern" for Pattern
Line 3,107: Line 3,107:
}
}
System.print("Displayed %(limit) pages with a total of %(totalCount) entries.")
System.print("Displayed %(limit) pages with a total of %(totalCount) entries.")
curl.easyCleanup()</lang>
curl.easyCleanup()</syntaxhighlight>
<br>
<br>
We now embed this script in the following C program, build and run.
We now embed this script in the following C program, build and run.
<lang c>/* gcc yahoo_search_interface.c -o yahoo_search_interface -lcurl -lwren -lm */
<syntaxhighlight lang="c">/* gcc yahoo_search_interface.c -o yahoo_search_interface -lcurl -lwren -lm */


#include <stdio.h>
#include <stdio.h>
Line 3,300: Line 3,300:
free(script);
free(script);
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}