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.
PrerequisitesStart using the Bing Entity Search API by creating one of the following Azure resources.
Bing Entity Search resourceCreate 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'
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)
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 a GET
request using request()
with your path and parameters, and header information.
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()
Call get_suggestions()
, and print the JSON response.
result = get_suggestions ()
print (json.dumps(json.loads(result), indent=4))
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
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