 <p>There are a couple of ways to handle the error. Since you are doing multiple lookups in a <code>dict</code>, wrapping it all in a <code>try/except</code> block is a good choice </p> <pre><code>import json import urllib def showsome(searchfor): query = urllib.urlencode({'q': searchfor}) url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&amp;%s' % query search_response = urllib.urlopen(url) search_results = search_response.read() results = json.loads(search_results) try: data = results['responseData'] print 'Total results: %s' % data['cursor']['estimatedResultCount'] hits = data['results'] print 'Top %d hits:' % len(hits) for h in hits: print ' ', h['url'] print 'For more results, see %s' % data['cursor']['moreResultsUrl'] except KeyError: print "I'm gettin' nuth'in man" showsome('"this is not searched searched in Google"') </code></pre>