使用Google Web API (Web Service) 构建本站搜索

1.       Google发布了一些API,用于向用户提供客户化的搜索.在自己的程序中,使用Google搜索服务,构建自己的搜索.

Google WEB API符合SOAPWSDL标准,它使用XML的方式返回结果,因此你可以自定义返回的结果显示方式,也可以方便的存储这些数据。

2.       使用Google Web API 步骤

2.1       注册Google帐号,获取license key. 才可用调用API.

如何获取license key 参见: http://www.google.com/apis/index.html

: WebAPI 对用户的搜索有限制,每天1000.

2.2       在自己的程序中添加对APIweb引用.

2.3       调用API中的方法,获取搜索结果

     googleweb服务就提供了三种方法:

doGoogleSearch(), doGetCachedPage(), doSpellingSuggestion().

doGoogleSearch()这个方法将返回和你输入的查询字符相符合的结果。然后这个方法返回一个GoogleSearchResult类的实例,这个实例中包含了搜索的结果。

要指定搜索某一个网站的信息,在搜索关键字后面加 site:网站地址 .

3.       问题

使用Google发布的WebAPI可以方便的实现自己站内的搜索.但有个问题是,你的网站在google中起码有个排名,也就是说你的网站要被google抓到,并且将你的网页存到它的知识库里.你才能从它的web api 中获取到对自己站内的搜索结果.

4.       自己做的例子(包括一个页面Search.aspx,cs文件Search.aspx.cs)

4.1       Search.aspx文件代码

    <form method="post" runat="server">

                    <table style="Z-INDEX: 101; LEFT: 24px; POSITION: absolute; TOP: 24px" width="1000" bgColor="#ffffff">

                             <tr>

                                       <td colSpan="3"><A><IMG src="img/search.gif" border="0"></A>&nbsp;

                                                <asp:textbox id="txb_query" runat="server"></asp:textbox>&nbsp;

                                                <asp:button id="btn_Go" runat="server" Text="搜索"></asp:button>&nbsp;

                                                <asp:CheckBox id="cbx_site" runat="server" Text="搜索本站"></asp:CheckBox>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                                                每天限制1000

                                       </td>

                             </tr>

                             <TR>

                                       <TD bgColor="#ccccff" colSpan="3"><FONT face="宋体"></FONT></TD>

                             </TR>

                             <TR>

                                       <TD style="HEIGHT: 21px" colSpan="3"><FONT face="宋体">[

                                                         <asp:label id="lab_total" runat="server" Width="8px">0</asp:label>]|</FONT><FONT face="宋体">搜索时间[

                                                         <asp:label id="lab_time" runat="server" Width="8px">0</asp:label>]&nbsp;&nbsp;&nbsp;&nbsp;

                                                         <asp:HyperLink id="HyperLink1" runat="server" NavigateUrl="/maxsoft/Default.aspx">返回主页</asp:HyperLink></FONT><FONT face="宋体"></FONT></TD>

                             </TR>

                             <TR>

                                       <TD bgColor="#ccccff" colSpan="3"><FONT face="宋体"></FONT><FONT face="宋体"></FONT><FONT face="宋体"></FONT></TD>

                             </TR>

                             <TR>

                                       <TD colSpan="3"><FONT face="宋体"></FONT><FONT face="宋体"></FONT><FONT face="宋体"><asp:placeholder id="PlaceHolder1" runat="server"></asp:placeholder></FONT></TD>

                             </TR>

                             <TR>

                                       <TD align="center" colSpan="3"><FONT face="宋体"><asp:linkbutton id="lbt_previous" runat="server">上一页</asp:linkbutton>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                                                         <asp:label id="lab_currentPage" runat="server">0</asp:label>/

                                                         <asp:label id="lab_totalPage" runat="server">0</asp:label>&nbsp;&nbsp;

                                                         <asp:LinkButton id="lbt_next" runat="server">下一页</asp:LinkButton></FONT></TD>

                             </TR>

                    </table>

           </form>

4.2       Search.aspx.cs文件代码

4.2.1            定义变量

private static int currentPage = 0;//当前页数

         private static int pageSize = 10;//每页多少条

         private static int totalPages = 0;//总页数

         private static int totalCount = 0;//总条数.

         private static string queryString = "";

private static string restrictSite = "www.sharptask.com/maxsoft";

4.2.2            搜索按钮代码

private void btn_Go_Click(object sender, System.EventArgs e)

         {

              this.InitializeVariable();

              this.GoSearch(pageSize,currentPage,this.txb_query.Text,restrictSite);

}

4.2.3            上一页 按钮

private void lbt_previous_Click(object sender, System.EventArgs e)

         {

              if(currentPage > 0)

                   --currentPage;

 

              this.GoSearch(pageSize,currentPage,queryString,restrictSite);

}

4.2.4            下一页 按钮

private void lbt_next_Click(object sender, System.EventArgs e)

         {

              ++ currentPage;

             this.GoSearch(pageSize,currentPage,queryString,restrictSite);

}

4.2.5            其他自定义方法

/// <summary>

         /// 初始化变量值

         /// </summary>

         private void InitializeVariable()

         {

              currentPage = 0;

              totalPages = 0;

              totalCount = 0;

              queryString = "";

         }

         /// <summary>

         /// 调用Google WEB API

         /// </summary>

         /// <param name="pageSize"></param>

         /// <param name="currentPage"></param>

         /// <param name="queryString">搜索条件</param>

         /// <param name="restrictSite">搜索站点</param>

         private void GoSearch(int pageSize,int currentPage,string queryString,string restrictSite)

         {

              Google.GoogleSearchService g = new maxsoft.Google.GoogleSearchService();

              try

              {

                   // Invoke the search method

                   Google.GoogleSearchResult r = g.doGoogleSearch(

                       "UsM/hRhQFHJiJj3cDXQ8sMbxPUZOpBYH",

                       (this.cbx_site.Checked ? queryString+" site:"+restrictSite : queryString),

                       this.GetCurrentPageStartIndex(), pageSize,

                       false,

                       "",

                       false, "lang_zh-CN", "", "");

                   // Extract the  numbers of results

                   GetResultNumbers(r);

                   //Bind element

                   this.BindElement(r);                

                  

              }

              catch (System.Web.Services.Protocols.SoapException ex)

              {

                   Response.Write(ex.Message);

              }

         }

         /// <summary>

         /// 计算点击按钮时要进入的页号.

         /// </summary>

         /// <returns></returns>

         private int GetCurrentPageStartIndex()

         {

              return currentPage * pageSize;

         }

         /// <summary>

         /// 计算总页数

         /// </summary>

         /// <returns></returns>

         private int GetTotalPages()

         {

              return ((totalCount%pageSize) == 0 ? totalCount/pageSize :totalCount/pageSize +1);

         }

         /// <summary>

         /// 获取搜索结果中的一些数,计算并显示到页面

         /// </summary>

         /// <param name="r"></param>

         private void GetResultNumbers(Google.GoogleSearchResult r)

         {

              totalCount = r.estimatedTotalResultsCount ;

              totalPages = GetTotalPages();

              queryString = r.searchQuery;

 

              //页面显示结果数据

              this.lab_total.Text = totalCount.ToString();

              this.lab_time.Text = r.searchTime.ToString();

              this.lab_totalPage.Text = totalPages.ToString();

              this.lab_currentPage.Text = (currentPage+1).ToString();

 

 }

posted @ 2005-11-25 00:08  meteorcui  阅读(510)  评论(0编辑  收藏  举报