python - 如何使用 tweepy 获取最新的用户状态

标签 python twitter tweepy

我正在尝试使用 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/

相关文章:

python - 使用 Raspberry & python 读取 USB RFID

Python Twitter API 如何搜索包含确切短语的推文?

php - 同步两个表 user_id 并感谢使用 twitter oauth 向用户确认

Synology 的 Python,如何安装 Python3 模块以及 Python2.7 安装在哪里?

python - MATLAB 的 place 函数有 python 版本吗

python - Numpy "fill"方法是如何实现的?

python - 更改多级层次结构中的索引 Pandas Dataframe

javascript - 即使在网站中使用 JavaScript 阻止弹出窗口时如何共享 Twitter

java - 为什么我所有的解码字符串最后都有 '?'? Java 字符串解码

python - 使用 Python 检索 Twitter 数据时出现 Unicode 解码错误