【问题标题】:how to get the lastest user status using tweepy如何使用 tweepy 获取最新的用户状态
【发布时间】:2013-03-04 10:40:03
【问题描述】:

我正在尝试使用 tweepy 获取最新的用户状态

我的代码是

api = tweepy.API(auth)
for status in tweepy.Cursor(api.user_timeline).items():
    lastid = status.id
    laststatus = api.get_status(lastid).text
    break

它有效。但我必须使用循环。 有没有更好的办法?

【问题讨论】:

    标签: python twitter tweepy


    【解决方案1】:

    .items() 返回一个迭代器,因此您可以简单地调用next() 来获取第一项:

    status = next(tweepy.Cursor(api.user_timeline).items())
    

    如果根本没有项目,这可能会引发StopIteration 异常。您可以将默认值添加到 next() 以防止发生这种情况:

    status = next(tweepy.Cursor(api.user_timeline).items(), None)
    

    【讨论】:

      猜你喜欢
      • 2022-10-05
      • 2021-07-25
      • 2022-12-11
      • 2011-03-16
      • 2022-01-18
      • 1970-01-01
      • 2016-07-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多