-
Notifications
You must be signed in to change notification settings - Fork 265
/
BingNewsSearchv7.py
38 lines (29 loc) · 1.02 KB
/
BingNewsSearchv7.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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# -*- coding: utf-8 -*-
import json
import os
from pprint import pprint
import requests
'''
This sample makes a call to the Bing News Search API with a text query and returns relevant news webpages.
Documentation: https: // docs.microsoft.com/en-us/azure/cognitive-services/bing-web-search/
'''
# Add your Bing Search V7 subscription key and endpoint to your environment variables.
subscriptionKey = os.environ['BING_SEARCH_V7_SUBSCRIPTION_KEY']
endpoint = os.environ['BING_SEARCH_V7_ENDPOINT'] + "/bing/v7.0/news/search"
query = "Microsoft"
# Construct a request
mkt = 'en-US'
params = {'q': query, 'mkt': mkt}
headers = {'Ocp-Apim-Subscription-Key': subscriptionKey}
# Call the API
try:
response = requests.get(endpoint, headers=headers, params=params)
response.raise_for_status()
print("\nHeaders:\n")
print(response.headers)
print("\nJSON Response:\n")
pprint(response.json())
except Exception as ex:
raise ex