2014-10-10 91 views
0

例如,从蟒蛇,Instagram的文档:提取标签

from instagram.client import InstagramAPI 

access_token = "YOUR_ACCESS_TOKEN" 
api = InstagramAPI(access_token=access_token) 
recent_media, next_ = api.user_recent_media(user_id="userid", count=10) 
for media in recent_media: 
    print media.caption.text # it gets caption text from each media 

但是,当我尝试这(仅最后一行更改):

from instagram.client import InstagramAPI 

access_token = "YOUR_ACCESS_TOKEN" 
api = InstagramAPI(access_token=access_token) 
recent_media, next_ = api.user_recent_media(user_id="userid", count=10) 
for media in recent_media: 
    print media.tags # attempt to get tags associated with each media 

它说,媒体没有“标签”属性。我认为Instagram API的响应包含“标签”键。

我错过了什么?如何获得媒体标签?

回答

0

首先,你应该确保有tags属性。像这样:

from instagram.client import InstagramAPI 

access_token = "YOUR_ACCESS_TOKEN" 
api = InstagramAPI(access_token=access_token) 
recent_media, next_ = api.user_recent_media(user_id="userid", count=10) 
for media in recent_media: 
    if hasattr(media, 'tags'): 
     print media.tags # attempt to get tags associated with each media 
    else: 
     # you can check if there is tags attribute through this line 
     print 'all the attributes of media are: ', repr(dir(media)) 
+0

所以,你是说如果发布该媒体的人没有写任何标签,那么不会有任何标签属性?它是否不返回空列表? – Jahongir 2014-10-10 09:32:29

+0

或者使用'tags = getattr(media,'tags',[])'或默认值empty'tags'应该采取 – 2014-10-10 09:33:42

+0

@Jahongir是的,我认为是。 – 2014-10-10 09:34:42