-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBingNewsSearchv7.py
More file actions
39 lines (29 loc) · 1.32 KB
/
BingNewsSearchv7.py
File metadata and controls
39 lines (29 loc) · 1.32 KB
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
#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/news/search"
term = "Microsoft"
def BingNewsSearch(search):
"Performs a Bing News 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")
print('Searching news for: ', term)
headers, result = BingNewsSearch(term)
print("\nRelevant HTTP Headers:\n")
print("\n".join(headers))
print("\nJSON Response:\n")
print(json.dumps(json.loads(result), indent=4))