2016-05-14 121 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)。當你沒有確認字典中是否存在密鑰時更爲可取。