raise ImgurClientError('JSON decoding of response failed.') imgurpython.helpers.error.ImgurClientError: JSON decoding of response failed

半世苍凉 提交于 2019-12-12 06:30:58

问题


For the code below:

 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 

I get :

$ 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.

Can you please suggest fixes?


回答1:


Maybe not the best solution but this solved the problem:

  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


来源:https://stackoverflow.com/questions/40099046/raise-imgurclienterrorjson-decoding-of-response-failed-imgurpython-helpers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!