特威顿率利米

2024-10-01 11:22:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我收到了一个“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

Tags: keytokenidappaccessstepcounttwitter