Python跟踪错误

时间:2015-05-22 00:42:25

标签: python-2.7 urllib2

无法使用此API创建响应。我无法调用函数locu_search(' new york')。我收到以下错误,如下所示。我使用Komodo作为我的IDE,这是在我创建一个新的python shell时开始的。

import urllib2
import json

local_api = '0d5897aae41eeafbd62ad0815af15cc42b2ed7c0'

def locu_search(query):
api_key = local_api
url = 'https://api.locu.com/v1_0/venue/search/?api_key=' + api_key
locality = query.replace('','%20')
final_url = url + "&locality=" + locality + "&category=restaurant"
json_obj = urllib2.urlopen(final_url)
data = json.load(json_obj)

for item in data['objects']:
    print item['name'],item['phone']

  locu_search('new york')

错误如下:

  **Traceback (most recent call last):
  File "<console>", line 0, in <module>
  File "<console>", line 0, in locu_search
  File "c:\python27\lib\urllib2.py", line 154, in urlopen
  return opener.open(url, data, timeout)
  File "c:\python27\lib\urllib2.py", line 437, in open
 response = meth(req, response)
 File "c:\python27\lib\urllib2.py", line 550, in http_response
'http', request, response, code, msg, hdrs)
 File "c:\python27\lib\urllib2.py", line 475, in error
 return self._call_chain(*args)
 File "c:\python27\lib\urllib2.py", line 409, in _call_chain
 result = func(*args)
 File "c:\python27\lib\urllib2.py", line 558, in http_error_default
 raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
 HTTPError: HTTP Error 400: BAD_REQUEST**

1 个答案:

答案 0 :(得分:0)

400错误请求应该给你一个关于问题的标题,这基本上是由于格式错误的请求而且我强烈怀疑罪魁祸首在url = 'https://api.locu.com/v1_0/venue/search/?api_key=' + api_key行,检查api_key令牌是否无效或不再有效。