Quickstart: Send a search request to Bing Entity Search REST API using Python
Use this quickstart to make your first call to 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
- Python 2.x or 3.x
Create and initialize the application
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.
import http.client, urllib.parse import json subscriptionKey = 'ENTER YOUR KEY HERE' host = 'api.bing.microsoft.com' path = '/v7.0/entities' mkt = 'en-US' query = 'italian restaurants near me'
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
Create a function called
get_suggestions()
.In this function, add your subscription key to a dictionary with
Ocp-Apim-Subscription-Key
as a key.Use
http.client.HTTPSConnection()
to create an HTTPS client object. Send aGET
request usingrequest()
with your path and parameters, and header information.Store the response with
getresponse()
, and returnresponse.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()
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"
},
. . .
]
}
}