【问题标题】:tweepy wait_on_rate_limit not workingtweepy wait_on_rate_limit 不起作用
【发布时间】:2016-09-11 22:48:14
【问题描述】:

所以,首先,我意识到在处理 twitter 速率限制方面存在许多问题。我不知道为什么,但到目前为止我发现没有一个对我有用。

我正在使用 tweepy。我正在尝试获取用户关注者的所有关注者的列表。正如预期的那样,由于 twitter 的速率限制,我无法一次将所有内容都拉下来。我安装了 tweepy v 3.5,因此指的是http://docs.tweepy.org/en/v3.5.0/api.html。要获取我使用的原始用户的关注者列表:

auth = tweepy.OAuthHandler(consumer_key,consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)

followerIDs = []
for page in tweepy.Cursor(api.followers_ids, screen_name=originatingUser, wait_on_rate_limit = True, wait_on_rate_limit_notify = True).pages():
    followerIDs.extend(page)

followers = api.lookup_users(follower)

这有点作用,但很快变成:

tweepy.error.TweepError: [{u'message': u'Rate limit exceeded', u'code': 88}]

我的理论是,然后使用这样的方法为每个 followerID 检索每个用户的关注者:

for followerID in followerIDs:
        for page in tweepy.Cursor(api.followers_ids, id=followerID, wait_on_rate_limit = True, wait_on_rate_limit_notify = True).pages():
                followerIDs.extend(page)

我遇到的另一个问题是当我尝试查找用户名时。为此,它使用 itertools 中的 grouper 函数将关注者分成 100 个组(api.lookup_users 一次只能接受 100 个 id)并使用

followerIDs = grouper(followerIDs,100)
for followerGroup in followerIDs:
        followerGroup=filter(None, followerGroup)
        followers = api.lookup_users(followerGroup,wait_on_rate_limit = True)
        for follower in followers:
                print (originatingUser + ", " + str(follower.screen_name))

得到一个不同的错误,即:

 TypeError: lookup_users() got an unexpected keyword argument 'wait_on_rate_limit'

这让我感到困惑,因为 tweepy api suggests 这应该是一个公认的论点。

关于我做错了什么有什么想法吗?

干杯 本。

【问题讨论】:

    标签: python twitter tweepy


    【解决方案1】:

    这里提到的 twitter API 有一个速率限制:https://dev.twitter.com/rest/public/rate-limiting

    通过此问题的快速解决方案可能是捕获速率限制错误并让您的应用程序休眠一段时间,然后从您离开的地方继续。

    pages = tweepy.Cursor(api.followers_ids, id=followerID).pages()
    while True:
        try:
            page = pages.next()
            followerIDs.extend(page)
        except TweepError:
            time.sleep(60 * 15)
            continue
        except StopIteration:
            break
    

    应该可以解决问题。不确定这是否会按您的预期工作,但基本想法是这样的。

    【讨论】:

    • 嗯,是的。我宁愿希望 wait_on_rate_limit 事情我做错了什么。这将是一个更加……优雅的解决方案。不过,这似乎是目前唯一可行的方法。
    • 对我来说听起来不太明智 - If an application abuses the rate limits, it will be blacklisted. Blacklisted apps are unable to get a response from the Twitter API
    【解决方案2】:

    我知道这可能有点晚了,但还是这样吧。

    您在 Cursor 构造函数中传递 wait_on_rate_limit 参数,而 tweepy 文档声明它应该在 API() 构造函数中传递。

    【讨论】:

      【解决方案3】:

      wait_on_rate_limit argument 将在 API() 构造函数中传递。 在您的代码中,它看起来像:

      api = tweepy.API(auth,wait_on_rate_limit=True)
      

      还有另一个参数wait_on_rate_limit_notify,它会在 tweepy 等待刷新速率限制时通知您。将两者都加起来最终会成行:

      api = tweepy.API(auth,wait_on_rate_limit=True,wait_on_rate_limit_notify=True)
      

      【讨论】:

        猜你喜欢
        • 2013-01-19
        • 2020-06-09
        • 2017-08-02
        • 2017-10-28
        • 1970-01-01
        • 1970-01-01
        • 2021-12-08
        • 1970-01-01
        • 2020-08-14
        相关资源
        最近更新 更多