Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试使用 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()以获取第一项:
.items()
next()
status = next(tweepy.Cursor(api.user_timeline).items())
如果根本没有项目,这可能会引发异常。StopIteration您可以添加一个默认值next()来防止这种情况:
StopIteration
status = next(tweepy.Cursor(api.user_timeline).items(), None)