A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://learn.microsoft.com/en-us/azure/cognitive-services/bing-entities-search/quickstarts/python below:

Quickstart: Send a search request to the REST API using Python - Bing Entity Search - Azure AI services

Quickstart: Send a search request to the Bing Entity Search REST API using Python

Use this quickstart to make your first call to the Bing Entity Search API and view the JSON response. This simple Python application sends a news search query to the API, and displays the response. The source code for this sample is available on GitHub.

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

Prerequisites Create an Azure resource

Start using the Bing Entity Search API by creating one of the following Azure resources.

Bing Entity Search resource Multi-Service resource Create and initialize the application
  1. Create a new Python file in your favorite IDE or editor, and add the following imports. Create variables for your subscription key, endpoint, market, and search query. 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 http.client, urllib.parse
    import json
    
    subscriptionKey = 'ENTER YOUR KEY HERE'
    host = 'api.bing.microsoft.com'
    path = '/v7.0/search'
    mkt = 'en-US'
    query = 'italian restaurants near me'
    
  2. Create a request url by appending your market variable to the ?mkt= parameter. Url-encode your query and append it to the &q= parameter.

    params = '?mkt=' + mkt + '&q=' + urllib.parse.quote (query)
    
Send a request and get a response
  1. Create a function called get_suggestions().

  2. In this function, add your subscription key to a dictionary with Ocp-Apim-Subscription-Key as a key.

  3. Use http.client.HTTPSConnection() to create an HTTPS client object. Send a GET request using request() with your path and parameters, and header information.

  4. Store the response with getresponse(), and return response.read().

    def get_suggestions ():
     headers = {'Ocp-Apim-Subscription-Key': subscriptionKey}
     conn = http.client.HTTPSConnection (host)
     conn.request ("GET", path + params, None, headers)
     response = conn.getresponse ()
     return response.read()
    
  5. Call get_suggestions(), and print the JSON response.

    result = get_suggestions ()
    print (json.dumps(json.loads(result), indent=4))
    
Example JSON response

A successful response is returned in JSON, as shown in the following example:

{
  "_type": "SearchResponse",
  "queryContext": {
    "originalQuery": "italian restaurant near me",
    "askUserForLocation": true
  },
  "places": {
    "value": [
      {
        "_type": "LocalBusiness",
        "webSearchUrl": "https://www.bing.com/search?q=sinful+bakery&filters=local...",
        "name": "Liberty's Delightful Sinful Bakery & Cafe",
        "url": "https://www.contoso.com/",
        "entityPresentationInfo": {
          "entityScenario": "ListItem",
          "entityTypeHints": [
            "Place",
            "LocalBusiness"
          ]
        },
        "address": {
          "addressLocality": "Seattle",
          "addressRegion": "WA",
          "postalCode": "98112",
          "addressCountry": "US",
          "neighborhood": "Madison Park"
        },
        "telephone": "(800) 555-1212"
      },

      . . .
      {
        "_type": "Restaurant",
        "webSearchUrl": "https://www.bing.com/search?q=Pickles+and+Preserves...",
        "name": "Munson's Pickles and Preserves Farm",
        "url": "https://www.princi.com/",
        "entityPresentationInfo": {
          "entityScenario": "ListItem",
          "entityTypeHints": [
            "Place",
            "LocalBusiness",
            "Restaurant"
          ]
        },
        "address": {
          "addressLocality": "Seattle",
          "addressRegion": "WA",
          "postalCode": "98101",
          "addressCountry": "US",
          "neighborhood": "Capitol Hill"
        },
        "telephone": "(800) 555-1212"
      },
      
      . . .
    ]
  }
}
Next steps

Build a single-page web app


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4