2016-05-14 51 views
0

这是我的代码如何获得tweepy api.get_user方法实体内的值(蟒蛇)

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 

输出:

entities : {u'description': {u'urls': [{u'url': u'https:/t.co/fdsfdsdsff', 
u'indices': [56, 79], u'expanded_url': u'http:/www.facebook.com/fsdfds', 
u'display_url': u'facebook.com/dfsfds'}, {u'url': u'https:/t.co/dfdsfdsfds', 
u'indices': [82, 105], u'expanded_url': u'http:/www.instagram.com/fsdfsdfds', 
u'display_url': u'instagram.com/dfdsfdsfds'}]}} 

我怎样才能得到的URL值的变量? ?

我试过了customerinfo.entities.description和customerinfo.entities.urls 但它不起作用。

+0

实体似乎是*字典*;你有没有试过用密钥访问它的内容?你得到的错误信息究竟是什么(*“不起作用”*很少继续)?你有没有读过图书馆文档? – jonrsharpe

+0

我是tweepy和python的新手,可以向我发送图书馆文档的链接。 –

+2

...你是新来的谷歌搜索的东西吗? http://www.tweepy.org/ – jonrsharpe

回答

0

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

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

等等。在提出问题之前检查文件通常是一种很好的做法。

+0

Tnku @Karol Thnku的建议。 :) –

+0

@Kasuni完全没问题:) –

0

customerinfo entitiesdict type.There的两种方式访问​​使用字典值:

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

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