编程语言
首页 > 编程语言> > python-Tweepy引用状态为“状态没有属性quoted_status”

python-Tweepy引用状态为“状态没有属性quoted_status”

作者:互联网

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

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

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

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

for status in timeline:
    print status.quoted_status

我也尝试过,但没有成功.

解决方法:

您可以检查对象是否具有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.)

标签:twitter,api,tweepy,python
来源: https://codeday.me/bug/20191026/1935428.html