【问题标题】:How to find POST friendships/create limit with tweepy in python 3?如何在 python 3 中使用 tweepy 查找 POST 友谊/创建限制?
【发布时间】:2020-01-18 14:43:22
【问题描述】:

我正在使用 tweepy 在 twitter 上关注用户,但我达到了 400 个每日限制,我想实施检查以防止这种情况发生,我正在使用以下代码:

limits = api.rate_limit_status()

这抓住了一些限制,但似乎不包括任何 POST 端点限制,仅 GET 端点。我的问题是如何使用 tweepy 访问 POST 端点限制?

谢谢

【问题讨论】:

    标签: python twitter tweepy


    【解决方案1】:

    api.rate_limit_status() 引用的 API 似乎不包括 POST 端点。我建议将您的 API 调用包装在如下所示的 try catch 中

    # In this example, the handler is time.sleep(15 * 60),
    # but you can of course handle it in any way you want.
    
    def limit_handled(cursor):
        while True:
            try:
                yield cursor.next()
            except tweepy.RateLimitError:
                time.sleep(15 * 60)
    
    for follower in limit_handled(tweepy.Cursor(api.followers).items()):
        if follower.friends_count < 300:
            print(follower.screen_name)
    

    另一种选择是手动跟踪您的 API 调用,并遵循here 中描述的限制。您可以为此使用另一个名为 ratelimit 的 python 模块

    pip install ratelimit
    
    # example.py:
    from ratelimit import limits
    
    import requests
    
    THREE_HOURS = 3*60*60
    FRIENDSHIP_POST_API_LIMIT = 300
    
    @limits(calls=FRIENDSHIP_POST_API_LIMIT, period=THREE_HOURS)
    def create_friendship(user_id):
        return api.create_friendship(user_id)
    

    【讨论】:

    • 谢谢,这看起来是最好的方法!
    猜你喜欢
    • 2013-01-27
    • 1970-01-01
    • 2019-05-12
    • 2018-02-28
    • 1970-01-01
    • 2015-12-12
    • 1970-01-01
    • 2012-08-22
    • 1970-01-01
    相关资源
    最近更新 更多