如何在python 3中使用tweepy查找POST友谊/创建限制?

问题描述 投票:0回答:1

我正在使用tweepy来跟踪Twitter上的用户,但我已经达到了每日400条的限制,我想实施检查,这样就不会发生,我正在使用以下代码:

limits = api.rate_limit_status()

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

谢谢

python twitter tweepy
1个回答
0
投票

似乎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)

另一个选择是按照here中所述的限制手动跟踪API调用。您可以为此使用另一个名为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)
© www.soinside.com 2019 - 2024. All rights reserved.