警告
在 2020 年 10 月 30 日,Bing 搜尋 API 從 Azure AI 服務移至 Bing 搜尋服務。 本檔僅供參考。 如需更新的檔,請參閱 Bing 搜尋 API 檔。 如需建立 Bing 搜尋新 Azure 資源的指示,請參閱 透過 Azure Marketplace 建立 Bing 搜尋資源。
使用此快速入門瞭解如何將搜尋要求傳送至 Bing 影像搜尋 API。 此 Python 應用程式會將搜尋查詢傳送至 API,並在結果中顯示第一個影像的 URL。 雖然此應用程式是以 Python 撰寫,但 API 是與大部分程式設計語言相容的 RESTful Web 服務。
先決條件
建立和初始化應用程式
在慣用的 IDE 或編輯器中建立新的 Python 檔案,並匯入下列模組。 為您的訂用帳戶密鑰、搜尋端點和搜尋字詞建立變數。 針對
search_url,您可以在下列程式代碼中使用全域端點,或使用 Azure 入口網站中針對您的資源顯示的 自定義子域 端點。import requests import matplotlib.pyplot as plt from PIL import Image from io import BytesIO subscription_key = "your-subscription-key" search_url = "https://api.cognitive.microsoft.com/bing/v7.0/images/search" search_term = "puppies"透過建立字典,將您的訂用帳戶密鑰作為值新增至
Ocp-Apim-Subscription-Key標頭。headers = {"Ocp-Apim-Subscription-Key" : subscription_key}
建立及傳送搜尋要求
建立搜尋要求參數的字典。 將搜尋字詞新增至
q參數。 將license參數設定為public,以搜尋公用網域中的影像。 將imageType設定為photo,只搜尋相片。params = {"q": search_term, "license": "public", "imageType": "photo"}使用
requests連結庫來呼叫 Bing 影像搜尋 API。 將標頭和參數新增至要求,並以 JSON 物件的形式傳回回應。 從回應的 [thumbnailUrl] 字段中取得數個縮圖影像的 URL。response = requests.get(search_url, headers=headers, params=params) response.raise_for_status() search_results = response.json() thumbnail_urls = [img["thumbnailUrl"] for img in search_results["value"][:16]]
檢視回應
使用 matplotlib 連結庫建立具有四個數據行和四個數據列的新圖表。
逐一遍歷圖形的行和列,並使用 PIL 函式庫的
Image.open()方法,將影像縮圖加入每個空間。使用
plt.show()繪製圖形並顯示影像。f, axes = plt.subplots(4, 4) for i in range(4): for j in range(4): image_data = requests.get(thumbnail_urls[i+4*j]) image_data.raise_for_status() image = Image.open(BytesIO(image_data.content)) axes[i][j].imshow(image) axes[i][j].axis("off") plt.show()
範例 JSON 回應
Bing 影像搜尋 API 的回應會以 JSON 的形式傳回。 此範例回應已截斷以顯示單一結果。
{
"_type":"Images",
"instrumentation":{
"_type":"ResponseInstrumentation"
},
"readLink":"images\/search?q=tropical ocean",
"webSearchUrl":"https:\/\/www.bing.com\/images\/search?q=tropical ocean&FORM=OIIARP",
"totalEstimatedMatches":842,
"nextOffset":47,
"value":[
{
"webSearchUrl":"https:\/\/www.bing.com\/images\/search?view=detailv2&FORM=OIIRPO&q=tropical+ocean&id=8607ACDACB243BDEA7E1EF78127DA931E680E3A5&simid=608027248313960152",
"name":"My Life in the Ocean | The greatest WordPress.com site in ...",
"thumbnailUrl":"https:\/\/tse3.mm.bing.net\/th?id=OIP.fmwSKKmKpmZtJiBDps1kLAHaEo&pid=Api",
"datePublished":"2017-11-03T08:51:00.0000000Z",
"contentUrl":"https:\/\/mylifeintheocean.files.wordpress.com\/2012\/11\/tropical-ocean-wallpaper-1920x12003.jpg",
"hostPageUrl":"https:\/\/mylifeintheocean.wordpress.com\/",
"contentSize":"897388 B",
"encodingFormat":"jpeg",
"hostPageDisplayUrl":"https:\/\/mylifeintheocean.wordpress.com",
"width":1920,
"height":1200,
"thumbnail":{
"width":474,
"height":296
},
"imageInsightsToken":"ccid_fmwSKKmK*mid_8607ACDACB243BDEA7E1EF78127DA931E680E3A5*simid_608027248313960152*thid_OIP.fmwSKKmKpmZtJiBDps1kLAHaEo",
"insightsMetadata":{
"recipeSourcesCount":0,
"bestRepresentativeQuery":{
"text":"Tropical Beaches Desktop Wallpaper",
"displayText":"Tropical Beaches Desktop Wallpaper",
"webSearchUrl":"https:\/\/www.bing.com\/images\/search?q=Tropical+Beaches+Desktop+Wallpaper&id=8607ACDACB243BDEA7E1EF78127DA931E680E3A5&FORM=IDBQDM"
},
"pagesIncludingCount":115,
"availableSizesCount":44
},
"imageId":"8607ACDACB243BDEA7E1EF78127DA931E680E3A5",
"accentColor":"0050B2"
}]
}