Advertisement
Thorin

Google Search AJAX API

Jun 26th, 2014
595
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ######################################################
  2. ###               Author: Thorin                   ###
  3. ###       Email:sushil.g.shenoy@gmail.com          ###
  4. ###              Date:27/06/2014                   ###
  5. ######################################################
  6.  
  7. #This Program uses the "Google Search AJAX API to
  8. #return search results of user-defined query
  9.  
  10. ######################################################
  11.  
  12. #!/usr/bin/environ python
  13. import urllib, simplejson
  14.  
  15. def Search_Function(query, number):
  16.     query = urllib.urlencode({'q':query})
  17.     index = number//4
  18.     if index%4!=0:index += 1 #To get more results than what user asked so that we dont fall short
  19.     for i in xrange(0,index):
  20.         url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&start='+str(index*4)+'&'+query
  21.         search_results = urllib.urlopen(url)
  22.         json = simplejson.loads(search_results.read())
  23.         results = json['responseData']['results']
  24.         for item in results:
  25.             if number == 0:
  26.                 break
  27.             print item['title'] + ": " + item['url']
  28.             number -= 1
  29.  
  30. if __name__ == "__main__":
  31.     query = raw_input('Please enter the query: ')
  32.     number = int(raw_input('Please enter the number of results: '))
  33.     Search_Function(query, number)
Advertisement
RAW Paste Data Copied
Advertisement