使用Big Huge Thesaurus API时出现Python错误

时间:2022-01-29 15:24:20

I'm building a synonym program in python. I have the API_KEY, I'm able to get JSON output

我正在python中构建一个同义词程序。我有API_KEY,我可以获得JSON输出

But for some words like 'ABRIGATE' and many more, I get this error on terminal

但是对于像'ABRIGATE'这样的词还有更多,我在终端上遇到了这个错误

Traceback (most recent call last):
  File "word_power.py", line 26, in <module>
    json_obj = urllib2.urlopen(final_url)
  File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 435, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 548, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 473, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 407, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 556, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 404: Not Found

Also on looking up with the urlin browser gives me a blank page.

另外,在查找urlin浏览器时,我会看到一个空白页面。

http://words.bighugelabs.com/api/2/------API_KEY------/abrigate/json

I guess I'm not able to detect if the JSON result exists or not.

我想我无法检测JSON结果是否存在。

The code with I'm checking is:

我正在检查的代码是:

json_obj = urllib2.urlopen(final_url)

        data = json.load(json_obj)

        if not data:
            print 'Empty'

        if 'noun' not in data:
            print 'NO NOUN'
            #target.write(" ]\n\n");

        if 'verb' not in data:
            print 'NO VERB'
            #target.write(" ]\n\n")

        if 'adjective' not in data:
            print 'NO ADJECTIVE'
            #target.write(" ]\n\n")

Please help

1 个解决方案

#1


1  

You need to catch those instance where the API returns an error:

您需要捕获API返回错误的那些实例:

data = None
try:
   json_obj = urllib2.urlopen(final_url)
   data = json.load(json_obj)
except urllib2.HTTPError, e:
   if e.code == '404':
      print('Entry not found for URL {}'.format(final_url))
   else:
      print('Got {} - {} for URL {}.'.format(e.code, e.msg, final_url))

You can also improve your checking logic:

您还可以改进检查逻辑:

for key,value in data.items():
  print('Word type is: {}'.format(key))
  for prop, values in value.items():
     print('\t{} : {}'.format(prop, ','.join(values)))

#1


1  

You need to catch those instance where the API returns an error:

您需要捕获API返回错误的那些实例:

data = None
try:
   json_obj = urllib2.urlopen(final_url)
   data = json.load(json_obj)
except urllib2.HTTPError, e:
   if e.code == '404':
      print('Entry not found for URL {}'.format(final_url))
   else:
      print('Got {} - {} for URL {}.'.format(e.code, e.msg, final_url))

You can also improve your checking logic:

您还可以改进检查逻辑:

for key,value in data.items():
  print('Word type is: {}'.format(key))
  for prop, values in value.items():
     print('\t{} : {}'.format(prop, ','.join(values)))