Yahoo! search interface: Difference between revisions
Content added Content deleted
(Removed page content for the moment) |
(Undo revision 31802 by Short Circuit (Talk)) |
||
Line 1: | Line 1: | ||
{{task|Programming environment operations}}[[Category:Networking and Web Interaction]] |
{{task|Programming environment operations}}[[Category:Networking and Web Interaction]] |
||
Create a class for searching Yahoo results. |
|||
'''This task is under consideration for deletion, as it enables violation of Google's Terms of Service. Please see its [[Talk:Search_Google|Talk Page]] to participate in the discussion. --[[User:Short Circuit|Short Circuit]] 12:43, 3 May 2009 (UTC) |
|||
It must implement a '''Next Page''' method, and read URL, Title and Content from results. |
|||
=={{header|C sharp|C#}}== |
|||
{{incorrect|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 TestSearch { |
|||
private string query; |
|||
private string content; |
|||
private int page = 1; |
|||
public GoogleSearch(string query) { |
|||
this.query = query; |
|||
this.content = new WebClient().DownloadString("http://www.test.com/search?q=" + query); |
|||
} |
|||
public TestSearch(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 <b>\\d+?</b> - <b>\\d+?</b> of about <b>(.+?)</b>"). |
|||
Match(this.content).Groups[1].Value.Replace(",", "")); |
|||
} |
|||
} |
|||
public GoogleResult[] Results { |
|||
get { |
|||
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)) { |
|||
string rurl = e.Groups[1].Value; |
|||
string rtitle = e.Groups[2].Value. |
|||
Replace("<em>", "").Replace("</em>", "").Replace(" <b>...</b>",""); |
|||
string rcontent = e.Groups[3].Value. |
|||
Replace("<em>", "").Replace("</em>", "").Replace(" <b>...</b>", ""); |
|||
Console.WriteLine(rurl); |
|||
results.Add(new TestResult(rurl, rtitle, rcontent)); |
|||
} |
|||
return (TestResult[])results.ToArray(typeof(TestResult)); |
|||
} |
|||
} |
|||
public GoogleSearch NextPage() { |
|||
return new TestSearch(this.query, this.page + 1); |
|||
} |
|||
public GoogleSearch GetPage(int page) { |
|||
return new TestSearch(this.query, page); |
|||
} |
|||
} |
|||
class TestResult { |
|||
public string URL { get; set; } |
|||
public string Title { get; set; } |
|||
public string Content { get; set; } |
|||
public TestResult(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> |
Revision as of 02:13, 4 May 2009
Yahoo! search interface
You are encouraged to solve this task according to the task description, using any language you may know.
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.
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 TestSearch {
private string query; private string content; private int page = 1;
public GoogleSearch(string query) { this.query = query; this.content = new WebClient().DownloadString("http://www.test.com/search?q=" + query); }
public TestSearch(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 GoogleResult[] 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 TestResult(rurl, rtitle, rcontent)); } return (TestResult[])results.ToArray(typeof(TestResult)); } }
public GoogleSearch NextPage() { return new TestSearch(this.query, this.page + 1); }
public GoogleSearch GetPage(int page) { return new TestSearch(this.query, page); }
}
class TestResult {
public string URL { get; set; } public string Title { get; set; } public string Content { get; set; }
public TestResult(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>