Twython速率限制

时间:2013-11-05 01:49:37

标签: python twitter twython

我收到了'TwythonRateLimitError',并希望确保我不会搞砸我的帐户。我是使用Twitter API的新手。如何检查以确保我没有超出查询限制?我读到这是每小时150次查询......如果我这样做会怎么样?我在代码中是否有这种风险,或者只针对特定命令?

我没有构建应用程序,我只是想获取一个特定的twitter样本(具有类似跟随基础的随机用户组(7500到10000个关注者)。到目前为止我的代码如下。我将保存成功点击文件,但我等着确定是必要的。

from twython import Twython, TwythonError, TwythonRateLimitError
from random import randint

APP_KEY = 'redacted'
APP_SECRET = 'redacted'
ACCESS_TOKEN = 'redacted'

twitter = Twython(APP_KEY, APP_SECRET, oauth_version=2)
ACCESS_TOKEN = twitter.obtain_access_token()

twitter = Twython(APP_KEY,access_token=ACCESS_TOKEN)

print "hello twitterQuery\n"

count = 0
step = 0
isError = 0
try:
    #new account i made today to set upper bound on userID
    maxID = twitter.show_user(screen_name="query_test")['id']
except TwythonRateLimitError:
    isError = 1
ids = [0,0,0,0,0,0,0,0,0,0]
if isError == 0 and step <= 150:
    while count < 10:
        step = step +1
        randomID = randint(1,maxID)
        isMissing = 0
        print str(step) + " " + str(randomID)
        try:
            randomUserData = twitter.show_user(user_id=randomID)
        except TwythonError:
            isMissing = 1;
        if isMissing == 0:
            followers = randomUserData['followers_count']
            if followers >= 7500 and followers <= 10000:
                print "ID: " + str(randomID) +", followers: "+ str(followers)
                ids[count] = randomID
                count = count+1

print "\ndone"
for each id in ids:
    print id

1 个答案:

答案 0 :(得分:2)

查看您当前的费率限制状态,传入您的应用令牌并发送GET请求

https://api.twitter.com/1.1/account/rate_limit_status.json

并查询回复。

有关更多背景信息,请参阅this page