Yahoo! search interface
Create a class for searching Yahoo results. It must implement a Next Page method, and read URL, Title and Content from results.
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
C#
<lang csharp>using System; using System.Net; using System.Text; using System.Text.RegularExpressions; using System.Collections; using System.Collections.Generic; using System.Linq;
class YahooSearch {
private string query; private string content; private int page = 1; public YahooSearch(string query) { this.query = query; this.content = new WebClient().DownloadString("http://www.test.com/search?q=" + query); } public YahooSearch(string query, int page) { this.query = query; this.page = page; this.content = new WebClient().DownloadString(String.Format("http://www.test.com/search?q={0}&start={1}", query, (this.page - 1) * 10)); } public int Page { get { return this.page; } } public int Length { get { return Int32.Parse(new Regex("Results \\d+? - \\d+? of about (.+?)"). Match(this.content).Groups[1].Value.Replace(",", "")); } } public YahooResult[] Results { get { ArrayList results = new ArrayList();
foreach (Match e in new Regex("
<a href=\"(.+?)\".+?>(.+?)</a>
(.+?)").Matches(this.content)) {
string rurl = e.Groups[1].Value; string rtitle = e.Groups[2].Value. Replace("", "").Replace("", "").Replace(" ...",""); string rcontent = e.Groups[3].Value. Replace("", "").Replace("", "").Replace(" ...", ""); Console.WriteLine(rurl); results.Add(new YahooResult(rurl, rtitle, rcontent)); } return (YahooResult[])results.ToArray(typeof(YahooResult)); } } public GoogleSearch NextPage() { return new YahooSearch(this.query, this.page + 1); } public GoogleSearch GetPage(int page) { return new YahooSearch(this.query, page); }
}
class YahooResult {
public string URL { get; set; } public string Title { get; set; } public string Content { get; set; } public YahooResult(string url, string title, string content) { this.URL = url; this.Title = title; this.Content = content; }
}</lang>
Usage:
<lang csharp>class Program {
static void Main() { TestSearch search = new TestSearch("rosetta code");
foreach (TestResult result in search.Results) { Console.WriteLine(result.Title); } }
}</lang>