【问题标题】:Tweepy - Get All Followers For Account - Rate Limit IssuesTweepy - 获取帐户的所有关注者 - 速率限制问题
【发布时间】:2017-02-06 22:08:26
【问题描述】:

以下是我的工作代码,用于获取某些帐户的 Twitter 关注者(在本例中为 @hudsonci)。

我的问题是吸引所有这些追随者所花费的时间。这个帐户特别有大约 1,000 名关注者……我一次只能达到 300 名,但有速率限制。因此,获得该帐户的所有关注者需要 > 一个小时。我可以想象这对于大客户来说将是一个巨大的痛苦。

我正在寻找一些关于如何改进这一点的建议。我觉得我没有充分利用分页光标,但我不能确定。

感谢任何帮助。

#!/usr/bin/env python
# encoding: utf-8

import tweepy 
import time 

#Twitter API credentials
consumer_key = "mine"
consumer_secret = "mine"
access_key = "mine"
access_secret = "mine"


#authorize twitter, initialize tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)

def handle_errors(cursor):
    while True:
        try:
            yield cursor.next()
        except tweepy.TweepError:
            time.sleep(20 * 60)

for user in handle_errors(tweepy.Cursor(api.followers,screen_name='hudsonci').items()):
     print user.screen_name

【问题讨论】:

    标签: python-2.7 twitter tweepy api-design rate-limiting


    【解决方案1】:

    根据the Twitter documentation for followers,您需要使用count 参数。

    指定尝试检索的 ID 数,每个不同请求最多 5,000 个。

    所以,添加count=5000 应该会对您有所帮助。

    【讨论】:

    • 将脚本更改为包含 for user in handle_errors(tweepy.Cursor(api.followers,screen_name='hudsonci', count= 5000).items()): 而我一次仍然只能获得 300 个关注者。计数是不是放错地方了?我觉得我在这里遗漏了一些非常简单的东西。
    • followers API 的限制是 200,所以你应该能够在任何 15 分钟间隔内获得 200 * 15 = 3000
    【解决方案2】:

    您一次获得 300 个关注者,因为获得关注者对象(而不是仅 ID)的页面限制为 20。每个窗口有 15 个请求,结果是 300 个关注者。

    以下是关注者的文档:https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-followers-list

    【讨论】:

      猜你喜欢
      • 2021-04-14
      • 2014-12-15
      • 2022-08-19
      • 2019-02-26
      • 2013-06-30
      • 2013-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多