从Instagram API接收的媒体中提取标签
这是来自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
它显示媒体没有“tags”这个属性。我觉得从Instagram的API返回的内容里应该有“tags”这个键。
我漏掉了什么呢?怎么才能获取媒体的标签呢?
1 个回答
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))