python - 在 tweepy 中捕获所有追随者

标签 python loops twitter tweepy

我想在 Twitter 中打印所有关注者或关注者:

while True:
    try:
       for user in tweepy.Cursor(api.followers,screen_name='TestUser').items():
            print user.screen_name
       break
    except tweepy.TweepError:
        time.sleep(60*20)

当我运行这部分时,它会 try catch 以下内容。在我的线程中捕获的用户数是 200。但它在 20 分钟休眠后不会继续......它会尝试但会再次捕获用户。

我该如何解决?

最佳答案

每次在 for user in... 行中创建新的迭代器时,每次 while 循环迭代都会重新开始。

尝试使用生成器:

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='TestUser').items()):
     print user.screen_name

关于python - 在 tweepy 中捕获所有追随者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33892529/

相关文章:

python - 检查 python 列表/numpy ndarray 中是否存在重复项的最快方法

java - 如何从文本文件中搜索名称

python - 在 For 循环中比较列表中的值; Python

iOS 使用 Fabric 发送推文而不撰写推文

SQL : SELECT w/MAX() and GROUP BY 的 Python 等效项

python - 如何从 Python 中的字符串中提取开始时间?

PHPMailer:大量电子邮件 - 一次发送所有可变消息,而不是单独发送

c# - 如何编写发布到 Twitter 的命令行 C# 程序

python - 使用 Python 的旧推文 Tweepy

python - 如何通过Python将时间戳值插入MongoDB上的ISODate类型?