Yahoo! search interface: Difference between revisions
Added Kotlin
No edit summary |
(Added Kotlin) |
||
Line 608:
}
}</lang>
=={{header|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).
<lang scala>// version 1.2.0
import java.net.URL
val rx = Regex("""<div class=\"yst result\">.+?<a href=\"(.*?)\" class=\"\">(.*?)</a>.+?class="abstract ellipsis">(.*?)</p>""")
class YahooResult(var title: String, var link: String, var text: String) {
override fun toString() = "\nTitle: $title\nLink : $link\nText : $text"
}
class YahooSearch(val query: String, val page: Int = 0) {
private val content: String
init {
val yahoo = "http://search.yahoo.com/search?"
val url = URL("${yahoo}p=$query&b=${page * 10 + 1}")
content = url.readText()
}
val results: MutableList<YahooResult>
get() {
val list = mutableListOf<YahooResult>()
for (mr in rx.findAll(content)) {
val title = mr.groups[2]!!.value.replace("<b>", "").replace("</b>", "")
val link = mr.groups[1]!!.value
val text = mr.groups[3]!!.value.replace("<b>", "").replace("</b>", "")
list.add (YahooResult(title, link, text))
}
return list
}
fun nextPage() = YahooSearch(query, page + 1)
fun getPage(newPage: Int) = YahooSearch(query, newPage)
}
fun main(args: Array<String>) {
for (page in 0..1) {
val x = YahooSearch("rosettacode", page)
println("\nPAGE ${page + 1} =>")
for (result in x.results.take(3)) println(result)
}
}</lang>
Output (restricted to first three results on first two pages):
<pre>
PAGE 1 =>
Title: Rosetta Code - Official Site
Link : http://rosettacode.org/wiki/Rosetta_Code
Text : Rosetta Code is a programming chrestomathy site. The idea is to present solutions to the same task in as ...
Title: Rosetta Code - Wikipedia
Link : https://en.wikipedia.org/wiki/Rosetta_Code
Text : Rosetta Code is a wiki-based programming chrestomathy website with implementations of common algorithms ...
Title: Rosetta Code (@rosettacode) | Twitter
Link : https://twitter.com/rosettacode
Text : The latest Tweets from Rosetta Code (@rosettacode). Twitter account for http://t.co/DuRZFWDfRn. The ...
PAGE 2 =>
Title: Rosetta Code Blog
Link : http://blog.rosettacode.org/
Text : As I noted, there was an expectation of downtime as the VPS hostRosetta Code sits on moved from one data ...
Title: Rosetta Code - Wikipedia
Link : https://en.wikipedia.org/wiki/User:Paddy3118/Rosetta_Code
Text : Rosetta Code is a wiki-based programming chrestomathy website with implementations of common algorithms ...
Title: Rosetta Code and ABAP | SAP Blogs
Link : https://blogs.sap.com/2015/03/27/rosetta-code-and-abap/
Text : Last week Christian Drumm (@ceedee666) and Fred Verheul (@fredverheul) had a short conversation on ...
</pre>
=={{header|Mathematica}}==
|