gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:56:38 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com