在中国用yahoo搜索的不多,但觉得yhoo还是不错的,它开放的搜索API最彻底,你可以搜索网页,图片,还有视频等等。当然,它也是要赢利的,在提供这些API之外,它还有有偿服务,也就是帮你做自己的搜索引擎。用之前最好了解一下它的说明:http://developer.yahoo.com/search/boss/ (哈~yahoo boss~yahoo老板)。
yhaoo网上有javascript之类的sample代码,一开始的时候yahoo的目的就是为了让人们应用到自己的网页上的。不过,只要js能做,C#也能的,找到了一个opensource的代码,但不记得是从哪里下载的了。我这里就给出它的web search的片段吧,因为我也只有这一部份的了,需要的朋友可以根据那些注释找到完整的代码。
=====================================================
WebSearchResponse.cs文件:
//------------------------------------------------------------------------------ // <autogenerated> // This code was generated by a tool. // Runtime Version: 1.1.4322.2032 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // </autogenerated> //------------------------------------------------------------------------------ // // This source code was auto-generated by xsd, Version=1.1.4322.2032. // namespace Yahoo.API.WebSearchResponse { using System.Xml.Serialization; /// <remarks/> [System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:yahoo:srch")] [System.Xml.Serialization.XmlRootAttribute(Namespace="urn:yahoo:srch", IsNullable=false)] public class ResultSet { /// <remarks/> [System.Xml.Serialization.XmlElementAttribute("Result")] public ResultType[] Result; /// <remarks/> [System.Xml.Serialization.XmlAttributeAttribute(DataType="integer")] public string totalResultsAvailable; /// <remarks/> [System.Xml.Serialization.XmlAttributeAttribute(DataType="integer")] public string totalResultsReturned; /// <remarks/> [System.Xml.Serialization.XmlAttributeAttribute(DataType="integer")] public string firstResultPosition; } /// <remarks/> [System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:yahoo:srch")] public class ResultType { /// <remarks/> public string Title; /// <remarks/> public string Summary; /// <remarks/> public string Url; /// <remarks/> public string ClickUrl; /// <remarks/> public string ModificationDate; /// <remarks/> public string MimeType; /// <remarks/> public CacheType Cache; } /// <remarks/> [System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:yahoo:srch")] public class CacheType { /// <remarks/> public string Url; /// <remarks/> public string Size; } }
======================================================
YahooSearchService.cs文件:
using System; using System.IO; using System.Net; using System.Text; using System.Web; using System.Xml.Serialization; namespace Yahoo.API { /// <summary> /// Summary description for Class1. /// </summary> public class YahooSearchService { public Yahoo.API.WebSearchResponse.ResultSet WebSearch(string appId, string query, string type, short results, int start, string format, bool adultOk, bool similarOk, string language) { string requestUri = String.Format("http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid={0}&query='{1}'&type={2}&results={3}&start={4}&format={5}&adult_ok={6}&similar_ok={7}&language={8}", appId, HttpUtility.UrlEncode(query, Encoding.UTF8), type, results, start, format, adultOk ? "1" : "0", similarOk ? "1" : "0", language); //System.Windows.Forms.MessageBox.Show(requestUri); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri); Yahoo.API.WebSearchResponse.ResultSet resultSet = null; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (Stream responseStream = response.GetResponseStream()) { XmlSerializer serializer = new XmlSerializer(typeof(Yahoo.API.WebSearchResponse.ResultSet)); resultSet = (Yahoo.API.WebSearchResponse.ResultSet)serializer.Deserialize(responseStream); } } return resultSet; } } }
=====================================================
using Yahoo.API; const string appid = "YahooExample"; YahooSearchService yahoo = new YahooSearchService(); Yahoo.API.WebSearchResponse.ResultSet resultSet = yahoo.WebSearch(appid, keywords, "any", count, page * count + 1, "all", true, true, "en"); foreach (Yahoo.API.WebSearchResponse.ResultType result in resultSet.Result) { result.Title result.Url 等都可以用了 }
有了这些,用户也就可以自己订制BOSS了!还要值得一提的就是那个appid,最好是网上申请一个,好简单的,比申请邮箱还简单,如果自己申请的appid,好像yahoo会提供一些个性化的搜索结果。
去这地址申请:
http://api.search.yahoo.com/webservices/register_application