Tag Archives: Azure Cognitive Services

ByHariharan Rajendran

Bing Image Search API – Python Script

This article explains how to use a python script to interact with Azure cognitive services – Bing Image Search.

Requirements

  1. Python 3 & libraries
  2. Bing Search API key – Azure Subscription

Bing Search API

Follow the below steps to create Bing Search API.

Step 1: Visit portal.azure.com

Step 2: Create a new resource – search for “Bing Search v7”

Step 3: Once created the API, you need to get API key. The base URL is same for everyone which is https://api.cognitive.microsoft.com/bing/v7.0/images/search

 

Python script

Use the below python script and replace the key.

import http.client, urllib.request, urllib.parse, urllib.error, base64

import requests, json

desc_url = ‘https://api.cognitive.microsoft.com/bing/v7.0/images/search’

headers = {

    # Request headers

    ‘Ocp-Apim-Subscription-Key’: ‘795bff7c586c43749d79a746dcebe12b’,

}

params = {

    # Request parameters

    ‘q’: ‘Hariharan Rajendran’,

    ‘count’: ‘5’,

    ‘offset’: ‘0’,

    ‘mkt’: ‘en-us’,

    ‘safeSearch’: ‘Moderate’,

}

desc_response = requests.get(

              desc_url, headers=headers, params=params)

desc=json.loads(desc_response.content)

print(desc)

You can change the parameter values to get the different result.

 

1