快速入门:使用必应新闻搜索 REST API 和 Go 获取新闻结果

警告

2020 年 10 月 30 日,必应搜索 API 从 Azure AI 服务迁移到必应搜索服务。 本文档仅供参考。 有关更新的文档,请参阅必应搜索 API 文档。 关于为必应搜索创建新的 Azure 资源的说明,请参阅通过 Azure 市场创建必应搜索资源

本快速入门使用 Go 语言调用必应新闻搜索 API。 结果包含查询字符串标识的新闻源的名称和 URL。

先决条件

  • 安装 Go 二进制文件
  • 安装 go-spew 库,以使用代码深度美化器显示结果。 使用此命令安装库:$ go get -u https://github.com/davecgh/go-spew

创建 Azure 资源

通过创建以下 Azure 资源之一开始使用必应新闻搜索 API:

必应搜索 v7 资源

  • 在删除资源前,可通过 Azure 门户使用。
  • 使用免费定价层试用该服务,稍后升级到用于生产的付费层。

多服务资源

  • 在删除资源前,可通过 Azure 门户使用。
  • 在多个 Azure AI 服务中对应用程序使用相同的密钥和终结点。

创建一个项目并导入库

在 IDE 或编辑器中新建一个 Go 项目。 然后,导入用于请求的 net/http,导入 ioutil 来读取响应,导入 encoding/json 来处理结果的 JSON 文本,导入 go-spew 库来分析 JSON 结果。

package main

import (
    "fmt"
    "net/http"
    "io/ioutil"
    "encoding/json"
	"github.com/davecgh/go-spew/spew"
)

创建一个结构来格式化新闻搜索结果

NewsAnswer 结构设置响应 JSON 中提供的数据的格式,该数据是多层且复杂的。 以下实现涵盖了基础知识:

// This struct formats the answer provided by the Bing News Search API.
type NewsAnswer struct {
    ReadLink       string `json: "readLink"` 
    QueryContext   struct {
        OriginalQuery   string   `json: "originalQuery"`
		AdultIntent     bool        `json: "adultIntent"`
	} `json: "queryContext"`
	TotalEstimatedMatches   int  `json: totalEstimatedMatches"` 
	Sort  []struct {
	    Name    string  `json: "name"`
		ID       string    `json: "id"`
		IsSelected       bool  `json: "isSelected"`
		URL      string   `json: "url"`
	}  `json: "sort"` 
	Value   []struct   {
	    Name     string   `json: "name"`
		URL   string    `json: "url"`
		Image   struct   {
		    Thumbnail   struct  {
			    ContentUrl  string  `json: "thumbnail"`
				Width   int  `json: "width"`
				Height  int   `json: "height"`
			} `json: "thumbnail"` 
		    } `json: "image"` 
			Description  string  `json: "description"`
			Provider  []struct   {
			    Type   string    `json: "_type"`
				Name  string     `json: "name"`
			} `json: "provider"` 
			DatePublished   string   `json: "datePublished"`
	} `json: "value"` 
}

声明主函数并定义变量

以下代码声明主函数并指定必需的变量。 确认终结点正确并将 token 值替换为来自你的 Azure 帐户的有效订阅密钥。 你可以使用以下代码中的全局终结点,或者使用资源的 Azure 门户中显示的自定义子域终结点。

func main() {
    // Verify the endpoint URI and replace the token string with a valid subscription key.  
    const endpoint = "https://api.cognitive.microsoft.com/bing/v7.0/news/search"
    token := "YOUR-ACCESS-KEY"
    searchTerm := "Microsoft Cognitive Services"

    // Declare a new GET request.
    req, err := http.NewRequest("GET", endpoint, nil)
    if err != nil {
        panic(err)
    }

    // The rest of the code in this example goes here in the main function.
}

查询和标头

添加查询字符串和访问密钥标头。

// Add the query to the request.  
param := req.URL.Query()
param.Add("q", searchTerm)
req.URL.RawQuery = param.Encode()

// Insert the subscription-key header.  
req.Header.Add("Ocp-Apim-Subscription-Key", token)

GET 请求

创建客户端并发送 GET 请求。

// Instantiate a client.  
client := new(http.Client)

// Send the request to Bing.  
resp, err := client.Do(req)
if err != nil {
    panic(err)
}

发送请求

使用 ioutil 发送请求并读取结果。

resp, err := client.Do(req)
    if err != nil {
	    panic(err)
}

// Close the connection.	
defer resp.Body.Close()

// Read the results
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
	panic(err)
}

处理响应

Unmarshall 函数从必应新闻搜索 API 返回的 JSON 文本提取信息。 然后,使用 go-spew 代码美化器显示结果中的节点。

// Create a new answer object 
ans := new(NewsAnswer)
err = json.Unmarshal(body, &ans)
if err != nil {
    fmt.Println(err)
}
	
fmt.Print("Output of BingAnswer: \r\n\r\n")
	
// Iterate over search results and print the result name and URL.
for _, result := range ans.Value{
spew.Dump(result.Name, result.URL)
}

结果

以下输出包含每个结果的名称和 URL:

(string) (len=91) "Cognitive Services Market: Global Industry Analysis and Opportunity Assessment, 2019 - 2025"
(string) (len=142) "https://www.marketwatch.com/press-release/cognitive-services-market-global-industry-analysis-and-opportunity-assessment-2019---2025-2019-02-21"
(string) (len=104) "Microsoft calls for greater collaboration to harness the power of AI to empower people with disabilities"
(string) (len=115) "https://indiaeducationdiary.in/microsoft-calls-greater-collaboration-harness-power-ai-empower-people-disabilities-2/"
(string) (len=70) "Microsoft 'Intelligent Cloud Hub' to build AI-ready workforce in India"
(string) (len=139) "https://cio.economictimes.indiatimes.com/news/cloud-computing/microsoft-intelligent-cloud-hub-to-build-ai-ready-workforce-in-india/67187807"
(string) (len=81) "Skills shortage is stopping many Asian companies from embracing A.I., study shows"
(string) (len=106) "https://www.cnbc.com/2019/02/20/microsoft-idc-study-skills-shortages-stopping-companies-from-using-ai.html"
(string) (len=143) "Cognitive Computing in Healthcare Market Emerging Top Key Vendors- Apixio, MedWhat, Healthcare X.0, Apple, Google, Microsoft, and IBM 2017-2025"
(string) (len=40) "http://www.digitaljournal.com/pr/4163064"
(string) (len=49) "Microsoft launches AI skills initiative in Brazil"
(string) (len=80) "https://www.zdnet.com/article/microsoft-launches-ai-skills-initiative-in-brazil/"
(string) (len=45) "Kuwait's CITRA and Microsoft host AI OpenHack"
(string) (len=70) "http://www.itp.net/618639-kuwaits-citra-and-microsoft-host-ai-openhack"
(string) (len=51) "CITRA and Microsoft collaborate to host AI workshop"
(string) (len=123) "https://www.zawya.com/mena/en/press-releases/story/CITRA_and_Microsoft_collaborate_to_host_AI_workshop-ZAWYA20190212105751/"

后续步骤