python - Tweepy 引用状态 "Status has no attribute quoted_status"

标签 python api twitter tweepy

我正在使用 python 并调用 api 请求。并非所有推文都有 quoted_status 字段,因为并非所有推文都被引用。我怎样才能克服这个错误

AttributeError: 'Status' object has no attribute 'quoted_status'

如果 quoted_status 不可用,则打印例如“null”?

我在循环中工作,我的实际代码是这样的:

for status in timeline:
    print status.quoted_status

我也试过 except 但没有成功。

最佳答案

您可以通过hasattr关键字检查对象是否具有该属性。

for status in timeline:
    if hasattr(status, 'quoted_status'):
        print (status.quoted_status)
    else:
        print ("null")

hasattr(对象,名称)

The arguments are an object and a string. The result is True if the string is the name of one of the object’s attributes, False if not. (This is implemented by calling getattr(object, name) and seeing whether it raises an exception or not.)

关于python - Tweepy 引用状态 "Status has no attribute quoted_status",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41923899/

相关文章:

python - Django 模型降序/嵌套查询

.NET/Entity Framework Core/SQL Server 过程返回重复值

ios - 从 iOS 6 在 Twitter 中发帖

ios - 使用 TwitterKit 在框架模块内包含非模块化 header

python - 在Python中将递归转换为迭代

python - 如何让多个 gevent 服务器永远服务?

python - 为什么这个 python "War"纸牌游戏只玩了几局就卡住了?

java - 反转颜色 OpenCV Java Api

javascript - 在 Angular JS 1.6 中处理 JSONP 响应

node.js - 如何进行 Twitter OAuth Echo 验证调用?