从Instagram API收到的媒体中提取标签

时间:2014-10-10 09:19:36

标签: python api instagram

来自github中的python-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的回复包括"标签"键。

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

1 个答案:

答案 0 :(得分:0)

首先,您应该确保有标签属性。像这样:

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))