【问题标题】:Avoiding rate limit error using tweepy使用 tweepy 避免速率限制错误
【发布时间】:2016-02-02 00:13:07
【问题描述】:

我正在尝试获取有关特定用户关注的所有 Twitter 朋友的一些基本信息。我正在使用 for 循环来获取此信息,但如果用户有很多朋友,我会收到速率限制错误。我正在尝试并努力将一种绕过速率限制的方法集成到我的 for 循环中。感谢您的任何建议!

我的原始代码:

data = []
for follower in followers:
    carrot = api.get_user(follower)
    data.append("[")
    data.append(carrot.screen_name)
    data.append(carrot.description)
    data.append("]")

我试图绕过速率限制错误:

data = []
for follower in followers:
    carrot = api.get_user(follower,include_entities=True)
    while True:
        try:
            data.append("[")
            data.append(carrot.screen_name)
            data.append(carrot.description)
            data.append("]")
        except tweepy.TweepError:
            time.sleep(60 * 15)
            continue
        except StopIteration:
            break

【问题讨论】:

标签: python for-loop tweepy rate-limiting


【解决方案1】:

问题可能是引发错误的可能是 get_user。尝试将 api.get_user 放入您的异常块中

代码如下。

data = []
for follower in followers:
    while True:
        try:
            carrot = api.get_user(follower,include_entities=True)
        except tweepy.TweepError:
            time.sleep(60 * 15)
            continue
        except StopIteration:
            pass
        break
    data.append([carrot.screen_name, carrot.description])

您打算如何存储这些值?以下是不是更容易使用

[约翰,旅行者]

与您的代码相反,将其存储为

[“[”,约翰,旅行者,“]”]

【讨论】:

    猜你喜欢
    • 2019-08-23
    • 2018-05-12
    • 1970-01-01
    • 2014-02-14
    • 2022-08-19
    • 2019-04-01
    • 2017-06-04
    • 1970-01-01
    • 2020-11-22
    相关资源
    最近更新 更多