urllib2.URLError:<urlopen error =“”[errno =“”8] =“”

时间:2017-08-17 21:07:51

标签: json python-2.7

=“”
 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 您想要寻找什么 ? &GT;&GT;苹果 Traceback(最近一次调用最后一次):   文件“/ Users / dragonleo / PycharmProjects / untitled2 / googleapi”,第8行,in     response = urllib2.urlopen(url + query).read()   文件“/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py”,第126行,在urlopen中     return _opener.open(url,data,timeout)   文件“/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py”,第391行,打开     response = self._open(req,data)   文件“/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py”,第409行,在_open中     '_open',req)   在_call_chain中输入文件“/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py”,第369行     result = func(* args)   在http_open中输入文件“/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py”,第1181行     return self.do_open(httplib.HTTPConnection,req)   在do_open中输入文件“/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py”,第1156行     提出URLError(错误) urllib2.URLError:

感谢专家能解释我收到错误的原因

1 个答案:

答案 0 :(得分:1)

立即突出两个问题:

  1. 上面的代码中有多个拼写错误。具体来说,括号和parens之间没有空格。此外,URL应为ajax.googleapis.com。
  2. Google Web Search API不再可用。您应该迁移到Google Custom Search API