Yahoo! search interface: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
(added c#)
Line 15: Line 15:
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Linq;

class YahooSearch {
class YahooSearch {
private string query;
private string query;
private string content;
private string content;
private int page = 1;
private int page = 1;

public YahooSearch(string query) {
public YahooSearch(string query) {
this.query = query;
this.query = query;
this.content = new WebClient().DownloadString("http://www.test.com/search?q=" + query);
this.content = new WebClient().DownloadString("http://search.yahoo.com/search?p=" + query);
}
}

public YahooSearch(string query, int page) {
public YahooSearch(string query, int page) {
this.query = query;
this.query = query;
this.page = page;
this.page = page;
this.content = new WebClient().DownloadString(String.Format("http://www.test.com/search?q={0}&start={1}", query, (this.page - 1) * 10));
this.content = new WebClient().DownloadString(String.Format("http://search.yahoo.com/search?p={0}&b={1}", query, ((this.page - 1) * 10) + 1));
}
}

public int Page {
public long Length {
get {
get {
return this.page;
return long.Parse(new Regex("<span id=\"infotext\">.+? of (.+?) for").
}
}
public int Length {
get {
return Int32.Parse(new Regex("Results <b>\\d+?</b> - <b>\\d+?</b> of about <b>(.+?)</b>").
Match(this.content).Groups[1].Value.Replace(",", ""));
Match(this.content).Groups[1].Value.Replace(",", ""));
}
}
}
}

public YahooResult[] Results {
public YahooResult[] Results {
get {
get {
ArrayList results = new ArrayList();
ArrayList results = new ArrayList();

foreach (Match e in new Regex("<li class=g><h3 class=r><a href=\"(.+?)\".+?>(.+?)</a></h3><div class=\"s\">(.+?)<cite>").Matches(this.content)) {
foreach (Match e in new Regex("<a class=\"yschttl spt\" href=\".+?\" >(.+?)</a></h3></div><div class=\"abstr\">(.+?)</div><span class=url>(.+?)</span>").Matches(this.content)) {
string rurl = e.Groups[1].Value;
string rurl = e.Groups[3].Value.
Replace("<b>", "").Replace("</b>", "").Replace("<wbr />", "").
string rtitle = e.Groups[2].Value.
Replace("<wbr>","");
string rtitle = e.Groups[1].Value.
Replace("<em>", "").Replace("</em>", "").Replace(" <b>...</b>","");
string rcontent = e.Groups[3].Value.
Replace("<b>", "").Replace("</b>", "").Replace("<wbr />","");
Replace("<em>", "").Replace("</em>", "").Replace(" <b>...</b>", "");
string rcontent = e.Groups[2].Value.
Replace("<b>", "").Replace("</b>", "").Replace("<b>...</b>", "").
Replace("<wbr />","");

Console.WriteLine(rurl);
Console.WriteLine(rurl);
results.Add(new YahooResult(rurl, rtitle, rcontent));
results.Add(new YahooResult(rurl, rtitle, rcontent));
Line 63: Line 59:
}
}
}
}

public GoogleSearch NextPage() {
public YahooSearch NextPage() {
return new YahooSearch(this.query, this.page + 1);
return new YahooSearch(this.query, this.page + 1);
}
}

public GoogleSearch GetPage(int page) {
public YahooSearch GetPage(int page) {
return new YahooSearch(this.query, page);
return new YahooSearch(this.query, page);
}
}
}
}

class YahooResult {
class YahooResult {
public string URL { get; set; }
public string URL { get; set; }
public string Title { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public string Content { get; set; }

public YahooResult(string url, string title, string content) {
public YahooResult(string url, string title, string content) {
this.URL = url;
this.URL = url;
Line 83: Line 79:
this.Content = content;
this.Content = content;
}
}
}
}</lang>


Usage:
//Usage:


<lang csharp>class Program {
class Prog {
static void Main() {
static void Main() {
TestSearch search = new TestSearch("rosetta code");
YahooSearch x = new YahooSearch("test");
Console.WriteLine(x.Results[0].Title); //Return first result title.

foreach (TestResult result in search.Results) {
Console.WriteLine(result.Title);
}
}
}
}</lang>
}</lang>

Revision as of 03:04, 4 May 2009

Task
Yahoo! search interface
You are encouraged to solve this task according to the task description, using any language you may know.

Create a class for searching Yahoo results. It must implement a Next Page method, and read URL, Title and Content from results. Instance lenght should return number of results.

C#

This example is incorrect. Please fix the code and remove this message.

Details: This examples is currently not working because Google TOS. It will be converted into Yahoo! later.

<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://search.yahoo.com/search?p=" + query);
   }
   public YahooSearch(string query, int page) {
       this.query = query;
       this.page = page;
       this.content = new WebClient().DownloadString(String.Format("http://search.yahoo.com/search?p={0}&b={1}", query, ((this.page - 1) * 10) + 1));
   }    
   public long Length {
       get {            
           return long.Parse(new Regex(".+? of (.+?) for").
               Match(this.content).Groups[1].Value.Replace(",", ""));
       }
   }
   public YahooResult[] Results {
       get {
           ArrayList results = new ArrayList();

foreach (Match e in new Regex("<a class=\"yschttl spt\" href=\".+?\" >(.+?)</a>

(.+?)

(.+?)").Matches(this.content)) {

               string rurl = e.Groups[3].Value.
                   Replace("", "").Replace("", "").Replace("", "").
                   Replace("","");
               string rtitle = e.Groups[1].Value.
                   Replace("", "").Replace("", "").Replace("","");
               string rcontent = e.Groups[2].Value.
                   Replace("", "").Replace("", "").Replace("...", "").
                   Replace("","");
               Console.WriteLine(rurl);
               results.Add(new YahooResult(rurl, rtitle, rcontent));
           }
           return (YahooResult[])results.ToArray(typeof(YahooResult));
       }
   }
   public YahooSearch NextPage() {
       return new YahooSearch(this.query, this.page + 1);
   }
   public YahooSearch 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;
   }

}

//Usage:

class Prog {

   static void Main() {
       YahooSearch x = new YahooSearch("test");
       Console.WriteLine(x.Results[0].Title); //Return first result title.
   }

}</lang>