Advertisement
Dinosawer

bing

Mar 23rd, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. def bing_search(query, search_type):
  2.     #search_type: Web, Image, News, Video
  3.     query = urllib.parse.quote(search_query)
  4.     # create credential for authentication
  5.     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)'
  6.     credentials = base64.b64encode (bytes(':%s' % key))[:-1]
  7.     auth = 'Basic %s' % credentials
  8.     url = 'https://api.datamarket.azure.com/Data.ashx/Bing/Search/image?Query=%27'+search_query+'%27&$top=5&$format=json'
  9.     request = urllib.request.Request(url)
  10.     request.add_header('Authorization', auth)
  11.     request.add_header('User-Agent', user_agent)
  12.     request_opener = urllib.request.build_opener()
  13.     response = request_opener.open(request)
  14.     response_data = response.read()
  15.     json_result = json.loads(response_data)
  16.     result_list = json_result['d']['results']
  17.     return result_list
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement