【问题标题】:imgurpython.helpers.error.ImgurClientRateLimitError: Rate-limit exceededimgurpython.helpers.error.ImgurClientRateLimitError:超出速率限制
【发布时间】:2017-02-28 03:34:06
【问题描述】:

我有以下错误:

/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 22, 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 153, in make_request
    raise ImgurClientRateLimitError()
imgurpython.helpers.error.ImgurClientRateLimitError: Rate-limit exceeded!

对于此代码:

  1 from imgurpython import ImgurClient
  2 import inspect
  3 import random
  4 import urllib2
  5 import requests
  6 from imgurpython.helpers.error import ImgurClientError
  7 
  8 client_id = "ABC"
  9 client_secret = "ABC"
 10 access_token = "ABC"
 11 refresh_token = "ABC"
 12 
 13 
 14 
 15 image_type = ['jpg', 'jpeg']
 16 
 17 imgur_client = ImgurClient(client_id, client_secret, access_token, refresh_token)
 18 
 19 item_count = 0
 20 for p in range(1, 10000):
 21     try:
 22         search = imgur_client.gallery_search('cat', window='all', sort='time', page=p)
 23         for i in range(0,https://gist.github.com/monajalal/e02792e9a5cbced301a8691b7a62836f len(search)):
 24             item_count +=1
 25             print(search[i].comment_count)
 26             if search[i].comment_count > 10 and not search[i].is_album:
 27                 print(search[i].type)
 28                 if search[i].type[6:] in image_type:
 29                     count = 0
 30                     try:
 31                         image_file = urllib2.urlopen(search[i].link, timeout = 5)
 32                         image_file_name = 'images/'+ search[i].id+'.'+search[i].type[6:]
 33                         output_image = open(image_file_name, 'wb')
 34                         output_image.write(image_file.read())
 35                         for post in imgur_client.gallery_item_comments(search[i].id, sort='best'):
 36                             if count <= 10:
 37                                 count += 1
 38                         output_image.close()
 39                     except urllib2.URLError as e:
 40                         print(e)
 41                         continue
 42                     except socket.timeout as e:
 43                         print(e)
 44                         continue
 45                     except socket.error as e:
 46                         print(e)
 47                         continue
 48     except ImgurClientError as e:
 49         print(e)
 50         continue
 51 
 52 print item_count

我也经常看到这条线:

/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

我该如何解决这个错误? Imgur中的速率限制错误有什么解决方法吗?所以我正在创建这个应用程序用于学术研究而不是商业用途,根据https://api.imgur.com/#limits 它应该是免费的,但我必须注册我的应用程序才能获得与 client_id 相关的东西。如何将我的应用程序设置为非商业应用,这样我就不会收到此速率限制错误,或者如果所有类型的应用程序都收到此错误,我应该如何处理它?我应该如何设置我的代码,使其每小时只发出 1250 个请求?

还有我的信用信息:

User Limit: 500
User Remaining: 500
User Reset: 2016-10-18 14:32:41
User Client Limit: 12500
User Client Remaining: 9570

更新:在答案中建议使用 sleep(8) 时,我最终会连续进行。对于不同的搜索查询,这发生在不同的页面上。如何修复代码以便在发生这种情况时停止执行?以下是更新的相关代码:https://gist.github.com/monajalal/e02792e9a5cbced301a8691b7a62836f

page number is: 157
0
image/jpeg
page number is: 157
0
page number is: 157
0
page number is: 157
/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
/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

【问题讨论】:

  • 请在 SO 上提出一个关于警告的新问题。

标签: python api http imgur rate-limiting


【解决方案1】:

速率限制是指您访问 API 的频率,而不是允许您调用的次数。为了防止锤击,大多数 API 都有速率限制(例如:每分钟 30 个请求,每 2 秒 1 个)。您的脚本正在尽可能快地发出请求,比限制快数百甚至数千倍。

为了防止你的脚本被锤击,最简单的解决方案是在你的 for 循环中引入一个sleep。

from time import sleep

for i in range(10000):
    print i
    sleep(2) # seconds

将睡眠时间调整为至少比 API 定义的速率限制大一秒。

https://api.imgur.com/#limits

Imgur API 使用信用分配系统来确保容量的公平分配。每个应用程序可以允许每天大约 1,250 次上传或每天大约 12,500 个请求。如果一个月内达到每日限制五次,则该应用程序将在本月剩余时间内被阻止。剩余的信用额度将在每个请求响应中显示在X-RateLimit-ClientRemaining HTTP 标头中。

所以 12500 个请求/24 小时是每小时 520 个请求或每分钟约 8 个请求。这意味着你的睡眠时间应该是 8 秒左右。

【讨论】:

  • 我知道我每小时只能发出 1250 个请求,但您对此估计需要多少睡眠时间?
  • 我接受了答案,因为它似乎对我有用 sleep(2)。如果我再次看到同样的错误,我会在稍后发表评论!谢谢:)
  • @MonaJalal 12500 / 24 = 每小时 520 个请求。通过粗略计算速率限制更新了我的答案。
  • 8 秒是最低速率限制,请将您的睡眠时间设置为sleep(8)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-12
  • 2012-08-10
  • 2018-08-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多