|
1 | | -import os, requests, json |
| 1 | +import http.client |
| 2 | +import json |
| 3 | +import os |
| 4 | +from pprint import pprint |
| 5 | +import requests |
| 6 | +import urllib.parse |
| 7 | + |
| 8 | +''' |
| 9 | +This sample uses the Bing Entity Search v7 to search for restaurants and return details about it. |
| 10 | +Bing Entity Search API: https://westus2.dev.cognitive.microsoft.com/docs/services/7a3fb374be374859a823b79fd938cc65/operations/52069701a465405ab3286f82 |
| 11 | +''' |
2 | 12 |
|
3 | 13 | # Add your Bing Entity Search subscription key and endpoint to your environment variables. |
4 | 14 | subscriptionKey = os.environ['BING_ENTITY_SEARCH_SUBSCRIPTION_KEY'] |
5 | 15 | host = os.environ['BING_ENTITY_SEARCH_ENDPOINT'] |
6 | | -path = '' |
7 | | -search_query = '' |
8 | | - |
9 | | -def entitySearch(): |
10 | | - |
11 | | - params = { |
12 | | - } |
13 | | - #the url to send the request |
14 | | - constructed_url = host + path |
15 | | - |
16 | | - headers = { |
17 | | - 'Ocp-Apim-Subscription-Key': subscriptionKey |
18 | | - } |
19 | | - |
20 | | - request = requests.get(constructed_url, headers=headers) |
21 | | - response = request.json() |
22 | | - |
23 | | - print(json.dumps(response, sort_keys=True, indent=4, ensure_ascii=False, separators=(',', ': '))) |
24 | | - |
25 | | -if __name__ = "__main__": |
26 | | - entitySearch() |
27 | | - |
28 | | - |
29 | | - |
30 | | - |
31 | | - |
32 | | - |
33 | | - |
34 | | - |
35 | | - |
36 | | - |
37 | | - |
38 | | - |
39 | | - |
40 | | - |
41 | | - |
42 | | - |
43 | | - |
44 | | - |
45 | | - |
46 | | - |
47 | | - |
48 | | - |
49 | | -# -*- coding: utf-8 -*- |
50 | | - |
51 | | -import http.client, urllib.parse |
52 | | -import json |
53 | | - |
54 | | -# ********************************************** |
55 | | -# *** Update or verify the following values. *** |
56 | | -# ********************************************** |
57 | | - |
58 | | -# Replace the subscriptionKey string value with your valid subscription key. |
59 | | -subscriptionKey = 'ENTER KEY HERE' |
60 | | - |
61 | | -host = 'api.cognitive.microsoft.com' |
| 16 | +host = host.replace('https://', '') |
62 | 17 | path = '/bing/v7.0/entities' |
63 | 18 |
|
64 | | -mkt = 'en-US' |
| 19 | +# Entity you want to find |
65 | 20 | query = 'italian restaurants near me' |
66 | 21 |
|
67 | | -params = '?mkt=' + mkt + '&q=' + urllib.parse.quote (query) |
68 | | - |
69 | | -def get_suggestions (): |
70 | | - headers = {'Ocp-Apim-Subscription-Key': subscriptionKey} |
71 | | - conn = http.client.HTTPSConnection (host) |
72 | | - conn.request ("GET", path + params, None, headers) |
73 | | - response = conn.getresponse () |
74 | | - return response.read () |
75 | | - |
76 | | -result = get_suggestions () |
77 | | -print (json.dumps(json.loads(result), indent=4)) |
| 22 | +# Construct a request |
| 23 | +mkt = 'en-US' |
| 24 | +params = '?mkt=' + mkt + '&q=' + urllib.parse.quote(query) |
| 25 | +headers = {'Ocp-Apim-Subscription-Key': subscriptionKey} |
| 26 | +conn = http.client.HTTPSConnection(host) |
| 27 | +conn.request("GET", path + params, None, headers) |
| 28 | + |
| 29 | +# Print response |
| 30 | +response = conn.getresponse() |
| 31 | +pprint(json.loads(response.read())) |
0 commit comments