#Copyright (c) Microsoft Corporation. All rights reserved. #Licensed under the MIT License. # -*- coding: utf-8 -*- import http.client, urllib.parse, json # ********************************************** # *** Update or verify the following values. *** # ********************************************** # Add your Bing Search V7 subscription key to your environment variables. subscriptionKey = os.environ['BING_SEARCH_V7_SUBSCRIPTION_KEY'] # Add your Bing Search V7 endpoint to your environment variables. host = os.environ['BING_SEARCH_V7_ENDPOINT'] path = "/bing/v7.0/search" term = "Microsoft Cognitive Services" def BingWebSearch(search): "Performs a Bing Web search and returns the results." headers = {'Ocp-Apim-Subscription-Key': subscriptionKey} conn = http.client.HTTPSConnection(host) query = urllib.parse.quote(search) conn.request("GET", path + "?q=" + query, headers=headers) response = conn.getresponse() headers = [k + ": " + v for (k, v) in response.getheaders() if k.startswith("BingAPIs-") or k.startswith("X-MSEdge-")] return headers, response.read().decode("utf8") if len(subscriptionKey) == 32: print('Searching the Web for: ', term) headers, result = BingWebSearch(term) print("\nRelevant HTTP Headers:\n") print("\n".join(headers)) print("\nJSON Response:\n") print(json.dumps(json.loads(result), indent=4)) else: print("Invalid Bing Search API subscription key!") print("Please paste yours into the source code.")