Sibi Shaju
Sibi Shaju

Reputation: 33

Instagram API using Python and collecting comments from each caption

I am currently trying out to use Instagram API using Python. I manage to collect comments from each caption, but I can only manage to collect a maximum of 8 comments from each caption. Some of the caption have 30+ comments, but I only collect the last 8 comments out of those 30+ comments.

Is there anything wrong with my code? Is there anyway to collect more than 8 comments per caption?

My codes are:

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

Upvotes: 1

Views: 3697

Answers (1)

ezcoding
ezcoding

Reputation: 2996

Your code is correct if you wanted to fetch the Media Objects only. If you're specifically targeting the comments, you'll have to fetch them with information from each Media Object individually.

From The Docs: Use the id of a Media Object to call the comments-api. In order to do that you should retrieve each id in your for-loop:

for media in recent_media:
  comments = api.media_comments(media.id)
  # do something with comments here

Upvotes: 1

Related Questions