Bing Web 搜索 SDK





5.00/5 (2投票s)
如何使用 Bing 搜索 SDK 以编程方式查找网页、新闻、图片、视频。
引言
Bing Web 搜索 SDK 使 Bing REST API 可供四种语言的应用程序使用。
- C#
- Java
- Node.js
- Python
背景
该 SDK 扩展了先前 Code Project 文章中介绍的 REST 接口。请参阅 Getting Started with the Bing Search APIs(开始使用 Bing 搜索 API)。
Using the Code
SDK 方法创建和管理 Web 请求。您无需解析 REST 接口返回的 JSON 文本。
要试验 Bing Web 搜索 SDK,您需要一个 API 访问密钥。 免费试用密钥可从 Azure: Try Cognitive Services(Azure:试用认知服务)获取。
使用 Bing Web 搜索 SDK 构建 C# 应用程序
要在 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
引用安装程序引用的库后,添加 using 指令
using Microsoft.Azure.CognitiveServices.Search.WebSearch;
using Microsoft.Azure.CognitiveServices.Search.WebSearch.Models;
创建 WebSearchAPI 的实例
创建搜索 client
。 ApiKeyServiceClientCredentials
构造函数将您的访问密钥作为 string
参数。
var client = new WebSearchAPI(new ApiKeyServiceClientCredentials("YOUR-ACCESS-KEY"));
搜索网页结果
使用上一个示例中创建的客户端,搜索网页
- 使用先前定义的查询调用
client.Web.Search
方法 - 检查结果
- 打印结果数量
- 打印第一个网页结果的名称和 URL
搜索 ‘Yosemite National Park
’(优胜美地国家公园),并打印结果。
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)
{
// find the first web page
var firstWebPagesResult = webData.WebPages.Value.FirstOrDefault();
if (firstWebPagesResult != null)
{
Console.WriteLine("Webpage Results #{0}", webData.WebPages.Value.Count);
Console.WriteLine("First web page name: {0} ", firstWebPagesResult.Name);
Console.WriteLine("First web page URL: {0} ", firstWebPagesResult.Url);
}
else
{
Console.WriteLine("Couldn't find web results!");
}
}
else
{
Console.WriteLine("Didn't see any Web data..");
}
}
Console.WriteLine("\r\nAny key to exit... ");
Console.ReadKey();
使用前几行代码,您可以运行使用 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");
//WebPages
if (webData?.WebPages?.Value?.Count > 0)
{
for (int w = 0; w < webData.WebPages.Value.Count; w++)
{
Console.WriteLine("Webpage Results #{0}", webData.WebPages.Value.Count);
Console.WriteLine("Web page name: {0} ", webData.WebPages.Value[w].Name);
Console.WriteLine("Web page URL: {0} ", 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)
{
// find the first image result
var firstImageResult = webData.Images.Value.FirstOrDefault();
if (firstImageResult != null)
{
Console.WriteLine("Image Results #{0}", webData.Images.Value.Count);
Console.WriteLine("First Image result name: {0} ", firstImageResult.Name);
Console.WriteLine("First Image result URL: {0} ", 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)
{
// find the first news result
var firstNewsResult = webData.News.Value.FirstOrDefault();
if (firstNewsResult != null)
{
Console.WriteLine("\r\nNews Results #{0}", webData.News.Value.Count);
Console.WriteLine("First news result name: {0} ", firstNewsResult.Name);
Console.WriteLine("First news result URL: {0} ", 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)
{
// find the first video result
var firstVideoResult = webData.Videos.Value.FirstOrDefault();
if (firstVideoResult != null)
{
Console.WriteLine("\r\nVideo Results #{0}", webData.Videos.Value.Count);
Console.WriteLine("First Video result name: {0} ", firstVideoResult.Name);
Console.WriteLine("First Video result URL: {0} ", firstVideoResult.ContentUrl);
}
else
{
Console.WriteLine("Couldn't find first video results!");
}
}
else
{
Console.WriteLine("Didn't see any video data..");
}
计数和偏移量
此示例搜索 ‘Best restaurants in 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)
{
// find the first web page
var firstWebPagesResult = webData.WebPages.Value.FirstOrDefault();
if (firstWebPagesResult != null)
{
Console.WriteLine("Web Results #{0}", webData.WebPages.Value.Count);
Console.WriteLine("First web page name: {0} ", firstWebPagesResult.Name);
Console.WriteLine("First web page URL: {0} ", 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<string> responseFilterstrings = new List<string>() { "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)
{
// find the first news result
var firstNewsResult = webData.News.Value.FirstOrDefault();
if (firstNewsResult != null)
{
Console.WriteLine("News Results #{0}", webData.News.Value.Count);
Console.WriteLine("First news result name: {0} ", firstNewsResult.Name);
Console.WriteLine("First news result URL: {0} ", 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);
}
}
答案计数和推广参数
此示例搜索查询 ‘susan sarandon
’(苏珊·莎兰登),并使用 answerCount
(答案计数)和 promote
(推广)参数并打印详细信息。
public static void WebSearchWithAnswerCountPromoteAndSafeSearch(WebSearchAPI client)
{
try
{
IList<string> promoteAnswertypeStrings = new List<string>() { "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 #{0}", webData.Videos.Value.Count);
Console.WriteLine("First Video result name: {0} ", firstVideosResult.Name);
Console.WriteLine("First Video result URL: {0} ", 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);
}
}
后续步骤
有关更多示例和代码以及其他编程语言,请参阅 Bing Search SDK preview(Bing 搜索 SDK 预览版)。
历史
- 第一版