在本文中,将探讨如何使用Bing搜索API在不同的编程语言中构建应用程序。Bing搜索API提供了一种强大的方法来搜索网页、图像、新闻和视频。将通过C#、Java、Node.js和Python这四种语言来展示如何实现这一功能。
Bing搜索API的SDK扩展了之前Code Project文章中介绍的REST接口。要开始使用Bing搜索API,需要一个API访问密钥,可以从Azure的“试用认知服务”中获得一个免费的试用密钥。
使用SDK方法可以创建和管理Web请求,不需要解析REST接口返回的JSON文本。以下是在C#中使用Bing Web搜索SDK的步骤。
要在Visual Studio中安装Bing Web搜索SDK,需要在解决方案资源管理器中右键单击项目,然后选择“NuGet包管理器”。点击“浏览”,并输入'websearch'。安装Microsoft.Azure.CognitiveServices.Search.WebSearch客户端库。
安装WebSearch SDK包也会安装依赖项,包括Newtonsoft.Json、Microsoft.Rest.ClientRuntime.2.3.10和Microsoft.Rest.ClientRuntime.Azure.3.3.10。
创建搜索客户端。ApiKeyServiceClientCredentials构造函数接受访问密钥作为字符串参数。
var client = new WebSearchAPI(new ApiKeyServiceClientCredentials("YOUR-ACCESS-KEY"));
使用前面示例中创建的客户端搜索网页:
var webData = client.Web.Search(query: "Yosemite National Park");
检查结果:
Console.WriteLine("Searched for Query# \" Yosemite National Park \"\r\n");
if(webData?.WebPages?.Value?.Count > 0)
{
var firstWebPagesResult = webData.WebPages.Value.FirstOrDefault();
if(firstWebPagesResult != null)
{
Console.WriteLine("Webpage Results #" + webData.WebPages.Value.Count);
Console.WriteLine("First web page name: " + firstWebPagesResult.Name);
Console.WriteLine("First web page URL: " + firstWebPagesResult.Url);
}
else
{
Console.WriteLine("Couldn't find web results!");
}
}
else
{
Console.WriteLine("Didn't see any Web data..");
}
以上代码展示了如何使用Bing搜索API运行应用程序。可以使用以下代码获取所有网页结果:
static void Main(string[] args)
{
var client = new WebSearchAPI(new ApiKeyServiceClientCredentials("19aa718a79d6444daaa415981d9f54ad"));
var webData = client.Web.Search(query: "Yosemite National Park");
Console.WriteLine("Searched for Query# \" Yosemite National Park \" \r\n");
if(webData?.WebPages?.Value?.Count > 0)
{
for(int w = 0; w < webData.WebPages.Value.Count; w++)
{
Console.WriteLine("Webpage Results #" + webData.WebPages.Value.Count);
Console.WriteLine("Web page name: " + webData.WebPages.Value[w].Name);
Console.WriteLine("Web page URL: " + webData.WebPages.Value[w].Url);
}
}
else
{
Console.WriteLine("Didn't see any Web data..");
}
Console.WriteLine("\r\nAny key to exit...");
Console.ReadKey();
}
以下代码与第一个示例类似,用于查找图像、新闻和视频结果。
// Images
if(webData?.Images?.Value?.Count > 0)
{
var firstImageResult = webData.Images.Value.FirstOrDefault();
if(firstImageResult != null)
{
Console.WriteLine("Image Results #" + webData.Images.Value.Count);
Console.WriteLine("First Image result name: " + firstImageResult.Name);
Console.WriteLine("First Image result URL: " + firstImageResult.ContentUrl);
}
else
{
Console.WriteLine("Couldn't find first image results!");
}
}
else
{
Console.WriteLine("Didn't see any image data..");
}
// News
if(webData?.News?.Value?.Count > 0)
{
var firstNewsResult = webData.News.Value.FirstOrDefault();
if(firstNewsResult != null)
{
Console.WriteLine("\r\nNews Results #" + webData.News.Value.Count);
Console.WriteLine("First news result name: " + firstNewsResult.Name);
Console.WriteLine("First news result URL: " + firstNewsResult.Url);
}
else
{
Console.WriteLine("Couldn't find any News results!");
}
}
else
{
Console.WriteLine("Didn't see first news data..");
}
// Videos
if(webData?.Videos?.Value?.Count > 0)
{
var firstVideoResult = webData.Videos.Value.FirstOrDefault();
if(firstVideoResult != null)
{
Console.WriteLine("\r\nVideo Results #" + webData.Videos.Value.Count);
Console.WriteLine("First Video result name: " + firstVideoResult.Name);
Console.WriteLine("First Video result URL: " + firstVideoResult.ContentUrl);
}
else
{
Console.WriteLine("Couldn't find first video results!");
}
}
else
{
Console.WriteLine("Didn't see any video data..");
}
以下示例搜索'Seattle最佳餐厅',并设置结果的偏移量和限制。然后它验证结果数量,并打印第一个结果的名称和URL。
public static void WebResultsWithCountAndOffset(WebSearchAPI client)
{
try
{
var webData = client.Web.SearchAsync(query: "Best restaurants in Seattle", offset: 10, count: 20).Result;
Console.WriteLine("\r\nSearched for Query# \" Best restaurants in Seattle \"");
if(webData?.WebPages?.Value?.Count > 0)
{
var firstWebPagesResult = webData.WebPages.Value.FirstOrDefault();
if(firstWebPagesResult != null)
{
Console.WriteLine("Web Results #" + webData.WebPages.Value.Count);
Console.WriteLine("First web page name: " + firstWebPagesResult.Name);
Console.WriteLine("First web page URL: " + firstWebPagesResult.Url);
}
else
{
Console.WriteLine("Couldn't find first web result!");
}
}
else
{
Console.WriteLine("Didn't see any Web data..");
}
}
catch(Exception ex)
{
Console.WriteLine("Encountered exception. " + ex.Message);
}
}
以下示例搜索'school choice',并使用响应过滤器过滤新闻,然后打印详细信息。
public static void WebSearchWithResponseFilter(WebSearchAPI client)
{
try
{
IList responseFilterstrings = new List() { "news" };
var webData = client.Web.SearchAsync(query: "School choice", responseFilter: responseFilterstrings).Result;
Console.WriteLine("\r\nSearched for Query# \" School choice \" with response filters \"news\"");
// News
if(webData?.News?.Value?.Count > 0)
{
var firstNewsResult = webData.News.Value.FirstOrDefault();
if(firstNewsResult != null)
{
Console.WriteLine("News Results #" + webData.News.Value.Count);
Console.WriteLine("First news result name: " + firstNewsResult.Name);
Console.WriteLine("First news result URL: " + firstNewsResult.Url);
}
else
{
Console.WriteLine("Couldn't find first News results!");
}
}
else
{
Console.WriteLine("Didn't see any News data..");
}
}
catch(Exception ex)
{
Console.WriteLine("Encountered exception. " + ex.Message);
}
}
public static void WebSearchWithAnswerCountPromoteAndSafeSearch(WebSearchAPI client)
{
try
{
IList promoteAnswertypeStrings = new List() { "videos" };
var webData = client.Web.SearchAsync(query: "susan sarandon", answerCount: 2, promote: promoteAnswertypeStrings, safeSearch: SafeSearch.Strict).Result;
Console.WriteLine("\r\nSearched for Query# \" susan sarandon \"");
if(webData?.Videos?.Value?.Count > 0)
{
var firstVideosResult = webData.Videos.Value.FirstOrDefault();
if(firstVideosResult != null)
{
Console.WriteLine("Video Results #" + webData.Videos.Value.Count);
Console.WriteLine("First Video result name: " + firstVideosResult.Name);
Console.WriteLine("First Video result URL: " + firstVideosResult.ContentUrl);
}
else
{
Console.WriteLine("Couldn't find videos results!");
}
}
else
{
Console.WriteLine("Didn't see any data..");
}
}
catch(Exception ex)
{
Console.WriteLine("Encountered exception. " + ex.Message);
}
}