我正在使用tweepy来跟踪Twitter上的用户,但我已经达到了每日400条的限制,我想实施检查,这样就不会发生,我正在使用以下代码:
limits = api.rate_limit_status()
这具有一些限制,但似乎不包括任何POST终结点限制,仅包括GET终结点。我的问题是如何使用tweepy访问POST端点限制?
谢谢
似乎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)
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)