简体   繁体   English

Tweepy Hipchat API-除了速率限制?

[英]Tweepy Hipchat API - Except rate limit?

I have this code to get followers of a twitter user: 我有以下代码来获取Twitter用户的关注者:

followers=[]
for user in tweepy.Cursor(api.followers,id=uNameInput).items():
    followers.append(user.screen_name)

However if this is used on a user with multiple followers, the script gets a rate limit and stops. 但是,如果将其用于具有多个关注者的用户,则脚本将获得速率限制并停止。 I would usually put this in a while true; 我通常会把这说成真的。 try, except else break loop but unsure where it would go in this instance. 尝试,除了其他中断循环,但不确定在这种情况下它将去哪里。

If you want to avoid rate limit, you can/should wait before the next follower page request: 如果您想避免速率限制,则可以/应该等待下一个关注者页面请求:

for user in tweepy.Cursor(api.followers, id=uNameInput).items():
    followers.append(user.screen_name)
    time.sleep(60)

Doesn't look beautiful, but should help. 看起来不漂亮,但应该有帮助。

UPD: According to the official twitter limits , you can make only 30 requests per 15-minute interval to get followers . UPD:根据官方的Twitter限制 ,您每15分钟只能发送30个请求才能获得followers

So, you can either catch rate limit exception and wait for 15 minutes interval to end, or define a counter and make sure you don't make more than 30 requests per 15-minute gap. 因此,您可以捕获速率限制异常并等待15分钟间隔结束,或者定义一个计数器并确保每15分钟间隔不超过30个请求。

Here's an example, how you can catch the tweepy exception and wait for 15 minutes before moving to the next portion of followers: 这是一个示例,说明如何捕获蠕虫异常并等待15分钟,然后再转到下一部分关注者:

import time
import tweepy

auth = tweepy.OAuthHandler(..., ...)
auth.set_access_token(..., ...)

api = tweepy.API(auth)
items = tweepy.Cursor(api.followers, screen_name="gvanrossum").items()

while True:
    try:
        item = next(items)
    except tweepy.TweepError:
        time.sleep(60 * 15)
        item = next(items)

    print item

Not sure this is the best approach though. 虽然不确定这是最好的方法。

UPD2: There is also another option: you can check for rate_limit_status , see how much requests remain for followers and decide whether to wait or continue. UPD2:还有另一个选项:您可以检查rate_limit_status ,查看对followers还有多少请求,然后决定等待还是继续。

Hope that helps. 希望能有所帮助。

There's a more precise way to do this with the new rate_limit_status' reset attribute. 使用新的rate_limit_status的reset属性,可以执行更精确的操作。 Whereas @alecxe's answer forces you to wait 15 minutes each time, even if the window is much smaller, you can instead wait just the right amount of time and no longer by doing: @alecxe的答案会迫使您每次等待15分钟,即使窗口很小,您也可以等待适当的时间,而不必这样做:

import time
import tweepy
import calendar
import datetime

auth = tweepy.OAuthHandler(..., ...)
auth.set_access_token(..., ...)

api = tweepy.API(auth)
items = tweepy.Cursor(api.followers, screen_name="gvanrossum").items()

while True:
    try:
        item = next(items)
    except tweepy.TweepError:
        #Rate limited. Checking when to try again
        rate_info = api.rate_limit_status()['resources']
        reset_time = rate_info['followers']['/followers/ids']['reset']
        cur_time = calendar.timegm(datetime.datetime.utcnow().timetuple())
        #wait the minimum time necessary plus a few seconds to be safe
        try_again_time = reset_time - cur_time + 5
        #Will try again in try_again_time seconds...
        time.sleep(try_again_time)

Here is my code 这是我的代码

try:
    followers=[]
    for user in tweepy.Cursor(api.followers,id=uNameInput).items():
        followers.append(user.screen_name)
except: 
    errmsg = str(sys.exc_info()[1])
    printdebug(errmsg)
    if errmsg.find("'code': 88") != -1: # [{'message': 'Rate limit exceeded', 'code': 88}]
        print("Blocked.")
        time.sleep(60 * 60) # Wait 1 hour for unblock
        pass
    else:
        raise

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM