поднять ImgurClientError(‘Не удалось выполнить декодирование ответа в формате JSON.’) imgurpython.помощники.ошибка.ImgurClientError: не удалось выполнить декодирование ответа в формате JSON

#python #json #api #exception #imgur

#python #json #API #исключение #imgur

Вопрос:

Для кода ниже:

  12 imgur_client = ImgurClient(client_id, client_secret, access_token, refresh_token)
 13 for p in range(100, 500):
 14     search = imgur_client.gallery_search('cat', window='all', sort='time', page=p)
 15     for i in range(0, len(search)):
 16         #print(search[i].link)
 17         #print(dir(search[i]))
 18         print(search[i].type)
 19         if search[i].comment_count > 30 and search[i].type!='gif':
 20             count = 0
 21             image_file = urllib2.urlopen(search[i].link, timeout = 5)
 22             image_file_name = 'images/'  search[i].id search[i].type
 23             output_image = open(image_file_name, 'wb')
 24             output_image.write(image_file.read())
 25 
 26             for post in imgur_client.gallery_item_comments(search[i].id, sort='best'):
 27                 if count <= 30:
 28                     count  = 1
 29 
  

Я получаю :

 $ python download.py 
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
Traceback (most recent call last):
  File "download.py", line 14, in <module>
    search = imgur_client.gallery_search('cat', window='all', sort='time', page=p)
  File "/usr/local/lib/python2.7/dist-packages/imgurpython/client.py", line 531, in gallery_search
    response = self.make_request('GET', 'gallery/search/%s/%s/%s' % (sort, window, page), data)
  File "/usr/local/lib/python2.7/dist-packages/imgurpython/client.py", line 158, in make_request
    raise ImgurClientError('JSON decoding of response failed.')
imgurpython.helpers.error.ImgurClientError: JSON decoding of response failed.
  

Не могли бы вы предложить исправления?

Ответ №1:

Возможно, это не лучшее решение, но это решило проблему:

   1 from imgurpython import ImgurClient
  2 import inspect
  3 import random
  4 import urllib2
  5 import requests
  6 from imgurpython.helpers.error import ImgurClientError

 15 imgur_client = ImgurClient(client_id, client_secret, access_token, refresh_token)
 16 
 17 for p in range(100, 500):
 18     try:
 19         search = imgur_client.gallery_search('cat', window='all', sort='time', page=p)
 20         for i in range(0, len(search)):
 21             if search[i].comment_count > 30 and not search[i].is_album:
 22                 print(search[i].type)
 23                 if 'jpg' in search[i].type :
 24                     count = 0
 25                     image_file = urllib2.urlopen(search[i].link, timeout = 5)
 26                     image_file_name = 'images/'  search[i].id '.' search[i].type[6:]
 27                     output_image = open(image_file_name, 'wb')
 28                     output_image.write(image_file.read())
 29                     for post in imgur_client.gallery_item_comments(search[i].id, sort='best'):
 30                         if count <= 30:
 31                             count  = 1
 32     except ImgurClientError as e:
 33         print(e)
 34         continue