-
Notifications
You must be signed in to change notification settings - Fork 0
/
bing.py
29 lines (27 loc) · 1.09 KB
/
bing.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
"""
A (very slightly) adapted version of Gugulethu Ncube's Bing Search API wrapper.
Original code from: http://www.guguncube.com/2771/python-using-the-bing-search-api
"""
import urllib
import urllib2
import json
import settings
def bing_search(query):
key = settings.BING_DEVELOPER_KEY
query = urllib.quote(query)
# create credential for authentication
user_agent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; FDM; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 1.1.4322)'
credentials = (':%s' % key).encode('base64')[:-1]
auth = 'Basic %s' % credentials
url = 'https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?Query=%27'+query+'%27&$top=5&$format=json'
request = urllib2.Request(url)
request.add_header('Authorization', auth)
request.add_header('User-Agent', user_agent)
request_opener = urllib2.build_opener()
response = request_opener.open(request)
response_data = response.read()
json_result = json.loads(response_data)
result_list = json_result['d']['results']
return result_list
if __name__ == "__main__":
main()