Quickstart: Use Python to call the Bing Web Search API

Warning

On October 30, 2020, the Bing Search APIs moved from Azure AI services to Bing Search Services. This documentation is provided for reference only. For updated documentation, see the Bing search API documentation. For instructions on creating new Azure resources for Bing search, see Create a Bing Search resource through the Azure Marketplace.

Use this quickstart to make your first call to the Bing Web Search API. This Python application sends a search request to the API, and shows the JSON response. Although this application is written in Python, the API is a RESTful Web service compatible with most programming languages.

This example is run as a Jupyter notebook on MyBinder. To run it, select the launch binder badge:

Binder

Prerequisites

Create an Azure resource

Start using the Bing Web Search API by creating one of the following Azure resources:

Bing Search v7 resource

  • Available through the Azure portal until you delete the resource.
  • Use the free pricing tier to try the service, and upgrade later to a paid tier for production.

Multi-service resource

  • Available through the Azure portal until you delete the resource.
  • Use the same key and endpoint for your applications, across multiple Azure AI services.

Define variables

  1. Replace the subscription_key value with a valid subscription key from your Azure account.

    subscription_key = "YOUR_ACCESS_KEY"
    assert subscription_key
    
  2. Declare the Bing Web Search API endpoint. You can use the global endpoint in the following code, or use the custom subdomain endpoint displayed in the Azure portal for your resource.

    search_url = "https://api.bing.microsoft.com/v7.0/search"
    
  3. Optionally, customize the search query by replacing the value for search_term.

    search_term = "Azure Cognitive Services"
    

Make a request

This code uses the requests library to call the Bing Web Search API and return the results as a JSON object. The API key is passed in the headers dictionary, and the search term and query parameters are passed in the params dictionary.

For a complete list of options and parameters, see Bing Web Search API v7.

import requests

headers = {"Ocp-Apim-Subscription-Key": subscription_key}
params = {"q": search_term, "textDecorations": True, "textFormat": "HTML"}
response = requests.get(search_url, headers=headers, params=params)
response.raise_for_status()
search_results = response.json()

Format and display the response

The search_results object includes the search results, and such metadata as related queries and pages. This code uses the IPython.display library to format and display the response in your browser.

from IPython.display import HTML

rows = "\n".join(["""<tr>
                       <td><a href=\"{0}\">{1}</a></td>
                       <td>{2}</td>
                     </tr>""".format(v["url"], v["name"], v["snippet"])
                  for v in search_results["webPages"]["value"]])
HTML("<table>{0}</table>".format(rows))

Sample code on GitHub

To run this code locally, see the complete sample available on GitHub.

Next steps

See also