As @Zloy Smiertniy pointed out, the answer can be found here.
However, if you are using Python 3 the syntax of raw_input
, urllib
has changed, and one has to decode the response
. Thus, for Python 3 one can use:
import urllib
import urllib.request
import json
url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&"
query = input("Query:")
query = urllib.parse.urlencode( {'q' : query } )
response = urllib.request.urlopen (url + query ).read()
data = json.loads ( response.decode() )
results = data [ 'responseData' ] [ 'results' ]
for result in results:
title = result['title']
url = result['url']
print ( title + '; ' + url )