如何在tweepy中获取实体内部的值api.get_用户方法(python)

2024-09-29 23:27:12 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的密码

consumerKey=" "
consumerSecret=" "
accessToken=" "
accessSecret=" "

auth = tweepy.OAuthHandler(consumerKey, consumerSecret)
auth.set_access_token(accessToken,accessSecret)

api = tweepy.API(auth)

customerinfo = api.get_user('@ABC')
print "entities :",customerinfo.entities

输出:

^{pr2}$

我怎样才能得到一个变量的url值???在

我试过了客户信息.实体.描述以及customerinfo.entities.url 但它不起作用。在


Tags: tokenauthapiurl密码accessentitiesset
2条回答

它不起作用,因为变量“entities”是“dictionary”类型。请看一下:https://docs.python.org/2/library/stdtypes.html部分:5.8。映射类型-dict。您需要通过“key”值获得访问权限,如下所示:

customerinfo.entities['description']
customerinfo.entities['description']['urls']

等等。在你问问题之前检查文档通常是一个很好的做法。在

customerinfo entities属于{}类型。那里有两种方法可以使用名称访问字典中的值:

a). customerinfo.entities['description']['urls']
b). customerinfo.entities.get('description').get('urls')

方法a)。如果不存在这样的键,将返回KeyError。 方法b)。如果不存在这样的键,将返回None。 因此,方法b)。在字典中没有密钥存在的确认时,更可取。在

相关问题 更多 >

    热门问题