{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Quickstart for Bing News Search API with Python\n", "This walkthrough demonstrates a simple example of calling into the Bing News Search API and post-processing the resulting JSON object. For more information, see [Bing New Search documentation](https://docs.microsoft.com/rest/api/cognitiveservices/bing-web-api-v7-reference). \n", "\n", "You can run this example as a Jupyter notebook on [MyBinder](https://mybinder.org) by clicking on the launch Binder badge: \n", "\n", "[![Binder](https://mybinder.org/badge.svg)](https://mybinder.org/v2/gh/Microsoft/cognitive-services-notebooks/master?filepath=BingNewsSearchAPI.ipynb)\n", "\n", "## Prerequisites\n", "\n", "You must have a [Cognitive Services API account](https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account) with **Bing Search APIs**. The [free trial](https://azure.microsoft.com/try/cognitive-services/?api=bing-web-search-api) is sufficient for this quickstart. You need the access key provided when you activate your free trial, or you may use a paid subscription key from your Azure dashboard.\n", "\n", "## Running the walkthrough\n", "First, set `subscription_key` to your API key for the Bing API service." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "subscription_key = None\n", "assert subscription_key" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, verify that the `search_url` endpoint is correct. At this writing, only one endpoint is used for Bing search APIs. If you encounter authorization errors, double-check this value against the Bing search endpoint in your Azure dashboard." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "search_url = \"https://api.cognitive.microsoft.com/bing/v7.0/news/search\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set `search_term` to look for news articles about Microsoft." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "search_term = \"Microsoft\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following block uses the `requests` library in Python to call out to the Bing search APIs and return the results as a JSON object. Observe that we pass in the API key via the `headers` dictionary and the search term via the `params` dictionary. To see the full list of options that can be used to filter search results, refer to the [REST API](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference) documentation." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import requests\n", "\n", "headers = {\"Ocp-Apim-Subscription-Key\" : subscription_key}\n", "params = {\"q\": search_term, \"textDecorations\": True, \"textFormat\": \"HTML\"}\n", "response = requests.get(search_url, headers=headers, params=params)\n", "response.raise_for_status()\n", "search_results = response.json()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `search_results` object contains the relevant new articles along with rich metadata. For example, the following line of code extracts the descriptions of the articles." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "descriptions = [article[\"description\"] for article in search_results[\"value\"]]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "These descriptions can then be rendered as a table with the search keyword highlighted in **bold**." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from IPython.display import HTML\n", "rows = \"\\n\".join([\"