我目前正在尝试使用 Python 使用 Instagram API。我设法从每个标题中收集评论,但我只能从每个标题中收集最多 8 条评论。有些标题有 30 多条评论,但我只收集了这 30 多条评论中的最后 8 条评论。
我的代码有什么问题吗?每个标题是否可以收集超过 8 条评论?
我的代码是:
from instagram.client import InstagramAPI
import re
access_token = "XXX"
client_secret = "XXX"
api = InstagramAPI(access_token=access_token, client_secret=client_secret)
recent_media, next_ = api.user_recent_media(user_id="476132155")
for media in recent_media:
try:
comments = media.comments
for i in comments:
print i.text, " --> ", i.user.username
print ""
except (UnicodeEncodeError, AttributeError, SyntaxError):
pass
最佳答案
如果您只想获取Media
对象,则您的代码是正确的。如果您专门针对评论,则必须分别从每个媒体
对象中获取它们的信息。
From The Docs :使用Media
对象的id
来调用comments-api。为此,您应该在 for 循环
中检索每个 id
:
for media in recent_media:
comments = api.media_comments(media.id)
# do something with comments here
关于python - Instagram API 使用 Python 并收集每个标题的评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30882998/