Home | Syndication | Delicious | Douban | Twitter | FriendFeed

Toy 2: Consuming Yahoo Search Web Services

与 Google 相比, Yahoo 提供的用于搜索方面的 web services 更多些,方方面面都涉及到了,包括 web, image, audio, video, news, local 等等,而且 Yahoo 好像也大度一点点,它对于查询量的限制是每天每个 IP 不多于5000次,算得上是慷慨了。另外值得一提的是, Yahoo Search Web Services 都是基于 REST 的,而非 SOAP ,比较时髦。

玩法一:Using Yahoo Search SDK

Yahoo 的这个 SDK 还是做的挺认真的,常见的开发语言几乎都支持,比如 Java, JavaScript, PHP, Perl, Python, Ruby, VB, C# 等,而且一直在更新,以下的这段示例片断是使用 v2.11 开发的。


public WebSearchResult[] search(String query, int start)
throws UnsupportedEncodingException, SearchException, IOException {
  String encodedQuery = URLEncoder.encode(query, "UTF-8");
  WebSearchRequest request = new WebSearchRequest(encodedQuery);
  request.setStart(BigInteger.valueOf(start));
  request.setResults(10);
  SearchClient client = new SearchClient("your_yahoo_appid");
  WebSearchResults wsr = client.webSearch(request);
  return wsr.listResults();
}

两点说明:
1,若查询中文,需要进行 UTF-8 进行编码。
2,Yahoo SDK 返回的结果集的位置是从1开始的,而 Google developer kit 的是从0开始计算。若打算集成这两者的结果集要留意这一差异。

玩法二:Dynamic Client

使用 axis2 写一个动态 web service 客户机来使用 Yahoo REST-style Search Web Service. 在开始之前需要知道 Yahoo 接受的请求参数和返回的结果集样式,在其开发者网站上有详细的描述

public OMElement search(String query, int start)
throws UnsupportedEncodingException, AxisFault {
  String epr = "http://search.yahooapis.com/WebSearchService/V1/webSearch";

  Options options = new Options();
  options.setTo(new EndpointReference(epr));
  options.setProperty(Constants.Configuration.ENABLE_REST,
              Constants.VALUE_TRUE);
  options.setProperty(Constants.Configuration.HTTP_METHOD,
              Constants.Configuration.HTTP_METHOD_GET);

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

  OMFactory fac = OMAbstractFactory.getOMFactory();
  OMNamespace ns = fac.createOMNamespace("urn:yahoo:srch", "");
  OMElement rootElement = fac.createOMElement("webSearch", ns);

  OMElement appId = fac.createOMElement("appid", ns, rootElement);
  appId.setText("your_yahoo_appid");

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

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

  return client.sendReceive(rootElement);
}

结果集就包含在返回的 XML 片断中。

This entry was posted on Wednesday, March 14th, 2007 at 17:37 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.

2 Responses to “Toy 2: Consuming Yahoo Search Web Services”

  • Candy Says at 22:14, March 14, 2007

    你的blog还是一如既往的不生活,哈!

  • richard Says at 23:12, March 14, 2007

    又被批评了,好,力争下一篇blog与技术无关,可以考虑贴贴美女啊、发发牢骚、讲讲八卦啊 blah blah blah…

Leave a Reply