AJAX is abused… (AJAX被滥用的趋势) 藏民为何转经 (zz)
Dec 04

Google Search API的英文全称是Google SOAP Search API,它使我们能够提交搜索请求到Google,并在程序中得到结构化的查询结果。在.NET中使用Google Search API非常方便,基本步骤是:

1. 到API上主页上,申请一个license key:
http://code.google.com/apis/soapsearch/index.html

2. 下载开发工具包(developer’s kit),如果不能下载,你需要登录先。

3. 解压缩开发工具包,里面含有Java、C#、VB.NET的示例程序。但这里我们只关心两个文件:

GoogleSearch.wsdl??
APIs_Reference.html

4. 使用.NET Framework SDK中的工具wsdl.exe生成本地proxy class:

wsdl.exe GoogleSearch.wsdl

这时生成一个C#文件:GoogleSearchService.cs

5. 在Visual Studio 2005中创建一个新的项目Project。把步骤4生成的GoogleSearchService.cs加入到项目中。

6. 下面一段代码完成一个查询:

GoogleSearchService gss =new GoogleSearchService();
GoogleSearchResult results = gss.doGoogleSearch(”your_License_Key“, “nicole kidman”, 0, 10, true, “”, false, “”, “”, “”);
foreach (ResultElement r in results.resultElements)
{
???Console.WriteLine(”Title: “+ r.title);
???Console.WriteLine(”Snippet: “+ r.snippet);
???Console.WriteLine(”URL: “+ r.URL);
???Console.WriteLine(”\n”);
}

7. OK,基本过程就是这样了。

?


需要特别注意的地方:
—————————————-
1. 对一个查询query,最多能能访问前1000个搜索结果。(MSN Search是前250个)

2. 每次搜索doGoogleSearch调用,最多只能返回10个搜索结果。(MSN Search是50个)

3. 如果搜索doGoogleSearch时,指定最多返回的结果数大于10,会引发SoapException。

4. 在很快的网络连接下,一个查询大概耗时6.6~10.3秒。批量执行时,每个查询大概耗时2.2秒。这样,取完1000个结果,大概耗时220秒。

5. 这里只列了一个最简单的例子,关于API的类、方法、参数说明,详细文档请参见APIs_Reference.html,里面解释的非常细致。

?


更多资源:

—————————————-
FAQ:http://code.google.com/apis/soapsearch/api_faq.html

Discussion/Community: http://groups.google.com/group/google.public.web-apis

More Gogole APIs: http://code.google.com/apis.html

MSN Search API: http://msdn.microsoft.com/live/msnsearch/default.aspx

随机日志

Leave a Reply