-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathweb_search_samples.py
170 lines (114 loc) · 5.35 KB
/
web_search_samples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
import os
from web_search_client import WebSearchClient
from web_search_client.models import SafeSearch
from azure.core.credentials import AzureKeyCredential
SUBSCRIPTION_KEY = None
ENDPOINT = "https://api.bing.microsoft.com" + "/v7.0/"
def result_types_lookup(subscription_key):
"""WebSearchResultTypesLookup.
This will look up a single query (Xbox) and print out name and url for first web, image, news and videos results.
"""
client = WebSearchClient(AzureKeyCredential(SUBSCRIPTION_KEY))
try:
web_data = client.web.search(query="xbox")
print('Searched for Query# " Xbox "')
# WebPages
if web_data.web_pages.value:
print("Webpage Results#{}".format(len(web_data.web_pages.value)))
first_web_page = web_data.web_pages.value[0]
print("First web page name: {} ".format(first_web_page.name))
print("First web page URL: {} ".format(first_web_page.url))
else:
print("Didn't see any Web data..")
# Images
if web_data.images.value:
print("Image Results#{}".format(len(web_data.images.value)))
first_image = web_data.images.value[0]
print("First Image name: {} ".format(first_image.name))
print("First Image URL: {} ".format(first_image.url))
else:
print("Didn't see any Image..")
# News
if web_data.news.value:
print("News Results#{}".format(len(web_data.news.value)))
first_news = web_data.news.value[0]
print("First News name: {} ".format(first_news.name))
print("First News URL: {} ".format(first_news.url))
else:
print("Didn't see any News..")
# Videos
if web_data.videos.value:
print("Videos Results#{}".format(len(web_data.videos.value)))
first_video = web_data.videos.value[0]
print("First Videos name: {} ".format(first_video.name))
print("First Videos URL: {} ".format(first_video.url))
else:
print("Didn't see any Videos..")
except Exception as err:
print("Encountered exception. {}".format(err))
def web_results_with_count_and_offset(subscription_key):
"""WebResultsWithCountAndOffset.
This will search (Best restaurants in Seattle), verify number of results and print out name and url of first result.
"""
client = WebSearchClient(AzureKeyCredential(SUBSCRIPTION_KEY))
try:
web_data = client.web.search(
query="Best restaurants in Seattle", offset=10, count=20
)
print('Searched for Query# " Best restaurants in Seattle "')
if web_data.web_pages.value:
print("Webpage Results#{}".format(len(web_data.web_pages.value)))
first_web_page = web_data.web_pages.value[0]
print("First web page name: {} ".format(first_web_page.name))
print("First web page URL: {} ".format(first_web_page.url))
else:
print("Didn't see any Web data..")
except Exception as err:
print("Encountered exception. {}".format(err))
def web_search_with_response_filter(subscription_key):
"""WebSearchWithResponseFilter.
This will search (Microsoft) with response filters to news and print details of news.
"""
client = WebSearchClient(AzureKeyCredential(SUBSCRIPTION_KEY))
try:
web_data = client.web.search(query="Microsoft", response_filter=["News"])
print('Searched for Query# " Microsoft " with response filters "News"')
# News attribute since I filtered "News"
if web_data.news.value:
print("Webpage Results#{}".format(len(web_data.news.value)))
first_web_page = web_data.news.value[0]
print("First web page name: {} ".format(first_web_page.name))
print("First web page URL: {} ".format(first_web_page.url))
else:
print("Didn't see any Web data..")
except Exception as err:
print("Encountered exception. {}".format(err))
def web_search_with_answer_count_promote_and_safe_search(subscription_key):
"""WebSearchWithAnswerCountPromoteAndSafeSearch.
This will search (Lady Gaga) with answerCount and promote parameters and print details of answers.
"""
client = WebSearchClient(AzureKeyCredential(SUBSCRIPTION_KEY))
try:
web_data = client.web.search(
query="Lady Gaga",
answer_count=2,
promote=["videos"],
safe_search=SafeSearch.strict, # or directly "Strict"
)
print('Searched for Query# " Lady Gaga"')
if web_data.web_pages.value:
print("Webpage Results#{}".format(len(web_data.web_pages.value)))
first_web_page = web_data.web_pages.value[0]
print("First web page name: {} ".format(first_web_page.name))
print("First web page URL: {} ".format(first_web_page.url))
else:
print("Didn't see any Web data..")
except Exception as err:
print("Encountered exception. {}".format(err))
if __name__ == "__main__":
result_types_lookup(SUBSCRIPTION_KEY)
web_results_with_count_and_offset(SUBSCRIPTION_KEY)
web_search_with_response_filter(SUBSCRIPTION_KEY)
web_search_with_answer_count_promote_and_safe_search(SUBSCRIPTION_KEY)