Quickstart: Perform a news search using Python and the Bing News Search REST 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 News Search API. This simple Python application sends a search query to the API and processes the JSON result.

Although this application is written in Python, the API is a RESTful Web service compatible with most programming languages.

To run this code sample as a Jupyter notebook on MyBinder, select the launch binder badge:

launch binder

The source code for this sample is also available on GitHub.

Create an Azure resource

Start using the Bing News 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.

Create and initialize the application

Create a new Python file in your favorite IDE or editor, and import the request module. Create variables for your subscription key, endpoint, and search term. You can use the global endpoint in the following code, or use the custom subdomain endpoint displayed in the Azure portal for your resource.

import requests

subscription_key = "your subscription key"
search_term = "Microsoft"
search_url = "https://api.cognitive.microsoft.com/bing/v7.0/news/search"

Create parameters for the request

Add your subscription key to a new dictionary, using Ocp-Apim-Subscription-Key as the key. Do the same for your search parameters.

headers = {"Ocp-Apim-Subscription-Key" : subscription_key}
params  = {"q": search_term, "textDecorations": True, "textFormat": "HTML"}

Send a request and get a response

  1. Use the requests library to call the Bing Visual Search API with your subscription key, and the dictionary objects you created in the previous step.

    response = requests.get(search_url, headers=headers, params=params)
    response.raise_for_status()
    search_results = json.dumps(response.json())
    
  2. Access the descriptions of the articles contained in the response from the API, which is stored in search_results as a JSON object.

    descriptions = [article["description"] for article in search_results["value"]]
    

Display the results

These descriptions can then be rendered as a table with the search keyword highlighted in bold.

from IPython.display import HTML
rows = "\n".join(["<tr><td>{0}</td></tr>".format(desc)
                  for desc in descriptions])
HTML("<table>"+rows+"</table>")

Next steps