Tweepy 引用状态 "Status has no attribute quoted_status"
Tweepy quoted status "Status has no attribute quoted_status"
我正在使用 python 并调用 api 请求。并非所有推文都有 quoted_status 字段,因为并非所有推文都被引用。我怎样才能克服错误
AttributeError: 'Status' object has no attribute 'quoted_status'
并打印 'null' 如果 quoted_status 不可用?
我在循环工作,我的实际代码是这样的:
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 并调用 api 请求。并非所有推文都有 quoted_status 字段,因为并非所有推文都被引用。我怎样才能克服错误
AttributeError: 'Status' object has no attribute 'quoted_status'
并打印 'null' 如果 quoted_status 不可用?
我在循环工作,我的实际代码是这样的:
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.)