Home | Syndication | Delicious | Douban | Twitter | FriendFeed

Toy 1: Developing Your Searcher with Google SOAP Search API

现在的主流搜索引擎厂商基本都非常 nice 地提供了适当的 web service 允许第三方应用集成其搜索能力和数据,当然,厂商们为了维护自己的商业利益,对使用这些 web service 也都做了相当严格的限制,比如,不允许用于商业用途、每个授权码(key/appid)每天最多只允许1000次查询,等等。因此,想要拿这些 web service 来开发大用户量的应用,那是不现实的,只能用来玩玩票。

本“玩具制造指南系列”从引擎老大 Google 开始。 Google SOAP Search API 支持三种操作: web 搜索、获取快照(缓存)、拼写建议。

玩法一:Using Google's developer kit

最简单的方法自然是使用 Google 官方提供的开发包,其中包含了对 Java, C#, VB 的支持。

public GoogleSearchResultElement[] search(String query, int start)
throws GoogleSearchFault {
  GoogleSearch gs = new GoogleSearch();
  gs.setKey("your_google_soap_search_key");
  gs.setStartResult(start);
  gs.setMaxResults(10);
  gs.setQueryString(query);
  GoogleSearchResult gsr = gs.doSearch();
  return gsr.getResultElements();
}

玩法二:Dynamic Client

从2006年12月5日开始, Google 不再受理有关 SOAP Search API 授权码(key)的申请,同时似乎也取消了其开发包的下载。不过我们依然可以使用 SOAP 实现来开发动态 web service 客户机。

在开始之前需要知道 Google 接受的请求样式和返回的结果集样式,在开发包soap-samples 目录下有样本 XML 文件。在 Google 网站上也有对 Search Request Format 的说明。

以下我使用的 SOAP 实现是 axis2 v1.1.1,虽然我没有专门尝试过,但我想其他的实现,比如 axis v1.x、xfire 等,应该都能很容易地完成类似的任务。

public OMElement search(String query, int start) throws AxisFault {
  Options options = new Options();
  options.setTo(new EndpointReference("http://api.google.com/search/beta2"));
  options.setProperty(Constants.Configuration.HTTP_METHOD,
               Constants.Configuration.HTTP_METHOD_POST);
  options.setProperty(HTTPConstants.HTTP_PROTOCOL_VERSION,
               HTTPConstants.HEADER_PROTOCOL_10);

  ServiceClient client = new ServiceClient();
  client.setOptions(options);

  OMFactory fac = OMAbstractFactory.getOMFactory();
  OMNamespace ns = fac.createOMNamespace("urn:GoogleSearch", "");
  OMElement rootElement = fac.createOMElement("doGoogleSearch", ns);

  OMElement appId = fac.createOMElement("key", ns, rootElement);
  appId.setText("your_google_soap_search_key");

  OMElement query = fac.createOMElement("q", ns, rootElement);
  query.setText(query);

  OMElement start = fac.createOMElement("start", ns, rootElement);
  start.setText(Integer.toString(start));

  OMElement maxResults = fac.createOMElement("maxResults", ns, rootElement);
  maxResults.setText("10");

  OMElement filter = fac.createOMElement("filter", ns, rootElement);
  filter.setText("false");

  OMElement restrict = fac.createOMElement("restrict", ns, rootElement);
  restrict.setText("");

  OMElement safeSearch = fac.createOMElement("safeSearch", ns, rootElement);
  safeSearch.setText("false");

  OMElement lr = fac.createOMElement("lr", ns, rootElement);
  lr.setText("");

  OMElement ie = fac.createOMElement("ie", ns, rootElement);
  ie.setText("UTF-8");

  OMElement oe = fac.createOMElement("oe", ns, rootElement);
  oe.setText("UTF-8");

  return client.sendReceive(rootElement);
}

返回的是一段 XML 片断,包含了 Google 搜索的结果。下面要做的事情就是分析这段 XML ,提取需要的值。

玩法三:Generating Web Service Client

本来这种方法比玩法二更好、也简单些,可惜的是当前的 axis2 还不支持 SOAP encoding style arrays ,所以无法从 Google WSDL 生成 client stub。

需要提到的是,我尝试用 axis v1.3 是可以从 Google WSDL 生成对应的 client stub 的,但是对中文支持存在问题,不知是否是 axis v1.3 自身的问题,我没有深究就放弃了。

This entry was posted on Monday, March 12th, 2007 at 19:44 and is filed under TechNotes. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply