1
import urllib2
import urllib
import json

url = "http://ajax/googleapis.com/ajax/services/search/web?v=1.0&"
query = raw_input ("What do you want to search for ? >> ")
query = urllib.urlencode({'q': query})
response = urllib2.urlopen (url + query).read()
data = json.loads (response)
results = data ['responseData'] ['results']
for result in results:
    title = result['title']
    url = result['url']
    print (title + ';' + url)

ERROR /System/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6 /Users/dragonleo/PycharmProjects/untitled2/googleapi What do you want to search for ? >> apple Traceback (most recent call last): File "/Users/dragonleo/PycharmProjects/untitled2/googleapi", line 8, in response = urllib2.urlopen (url + query).read() File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 126, in urlopen return _opener.open(url, data, timeout) File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 391, in open response = self._open(req, data) File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 409, in _open '_open', req) File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 369, in _call_chain result = func(*args) File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 1181, in http_open return self.do_open(httplib.HTTPConnection, req) File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 1156, in do_open raise URLError(err) urllib2.URLError:

Appreciate if expert can explain why I am getting the error

1
  • Added the last time in the error urllib2.URLError: <urlopen error [Errno 8] nodename nor servname provided, or not known>
    – dragonleo
    Aug 17, 2017 at 21:41

1 Answer 1

1

Two problems stand out immediately:

  1. There are multiple typos in the code above. Specifically, there are no spaces between brackets and parens. Also, the URL should be ajax.googleapis.com.
  2. The Google Web Search API is no longer available. You should migrate to the Google Custom Search API
1
  • Thanks for your suggestions.
    – dragonleo
    Aug 21, 2017 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.