【问题标题】:Twython rate limitTwython 速率限制
【发布时间】:2013-11-05 11:26:00
【问题描述】:

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

【问题讨论】:

    标签: python twitter twython


    【解决方案1】:

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

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

    并查询响应。

    请参阅this page 了解更多上下文

    【讨论】:

    • 我对此进行了调查,但不确定我是否理解。这是否意味着我最多可以查询任何用户 15 次?我在 Twitter 开发控制台中运行它,它从 15 变为 14,然后我运行我的应用程序并再次在控制台中运行它,并期望它对于每个查询都为 -1,但它实际上并没有下降。这是因为我只查询了一次我的开发用户名吗?但如果我运行它足够多,它会引用“query_user”帐户 15 次并用完尝试?
    • 它可能正在缓存您的请求。
    猜你喜欢
    • 2015-04-21
    • 1970-01-01
    • 2014-07-16
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 2020-11-04
    • 2021-01-22
    相关资源
    最近更新 更多