我正在尝试使用 tweepy 获取最新的用户状态
我的代码是
api = tweepy.API(auth)
for status in tweepy.Cursor(api.user_timeline).items():
lastid = status.id
laststatus = api.get_status(lastid).text
break
它有效。但我必须使用循环。 有没有更好的办法?
最佳答案
.items()
返回一个迭代器,因此您可以简单地调用 next()
来获取第一个项目:
status = next(tweepy.Cursor(api.user_timeline).items())
如果根本没有项目,这可能引发StopIteration
异常。您可以将默认值添加到 next()
以防止出现这种情况:
status = next(tweepy.Cursor(api.user_timeline).items(), None)
关于python - 如何使用 tweepy 获取最新的用户状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15437097/