【问题标题】:Extract tags from media received from Instagram API从 Instagram API 接收的媒体中提取标签
【发布时间】:2014-10-10 09:19:35
【问题描述】:

来自 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 的响应包括“标签”键。

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

【问题讨论】:

    标签: python api instagram


    【解决方案1】:

    首先,你应该确保有 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))
    

    【讨论】:

    • 那么,你是说如果发布该媒体的人没有写任何标签,就不会有任何标签属性?它不返回空列表吗?
    • 或者 - 使用 tags = getattr(media, 'tags', []) 或任何空的默认值 tags 应该采用
    • @Jahongir 是的,我想是的。
    • @m170897017 谢谢你,你是对的。我尝试了肯定有标签的媒体,它有效
    猜你喜欢
    • 2016-07-23
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多