【问题标题】:Tweepy quoted status "Status has no attribute quoted_status"Tweepy 引用状态“状态没有属性引用状态”
【发布时间】:2017-06-14 21:38:13
【问题描述】:

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

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

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

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

for status in timeline:
    print status.quoted_status

我也尝试了 except 但没有成功。

【问题讨论】:

    标签: python api twitter tweepy


    【解决方案1】:

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

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

    hasattr(对象,名称)

    参数是一个对象和一个字符串。结果为真,如果 string 是对象属性之一的名称,如果不是,则为 False。 (这是通过调用 getattr(object, name) 并查看 是否引发异常。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-22
      • 1970-01-01
      • 2015-10-03
      • 2019-10-24
      • 1970-01-01
      相关资源
      最近更新 更多