dragonleo
dragonleo

Reputation: 11

urllib2.URLError: <urlopen error [Errno 8]

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

Upvotes: 1

Views: 6026

Answers (1)

Lex Scarisbrick
Lex Scarisbrick

Reputation: 1570

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

Upvotes: 1

Related Questions