Search code examples
pythoninstagraminstaloader

Get list of users who commented or liked post on instagram with Python


I have discovered Instaloader lib in Python that allows to scrape Instagram profiles. Its very good, but I cant find a way to get list of users who commented or liked post on instagram.

I have looked all over the documentation but I cant find the answer. This is the documentation: https://instaloader.github.io/as-module.html

This is the code that I have:

import instaloader

L = instaloader.Instaloader() #nalazenje stvari sa instagrama

profile = instaloader.Profile.from_username(L.context, 'jlo') #daj mi pratioce od datog user-a
print(profile.get_posts())

for post in profile.get_posts():

    post_likes = post.get_likes()
    post_comments = post.get_comments()

    print(post_likes)  # post_likes object
    print(post_comments) # # post_comments object

    # post_likes.name  post_likes.username  post_likes.user    DOES NOT WORK
    # post_comments.name  post_comments.username  post_comments.user    DOES NOT WORK

Solution

  • The get_likes() yields an generator to iterate over the profiles of the accounts that liked a post.

    The get_comments() yields a named tuple with owner beeing the account of the poster. So a working implementation of your code would look something like this:

    import instaloader
    
    L = instaloader.Instaloader() #nalazenje stvari sa instagrama
    
    profile = instaloader.Profile.from_username(L.context, 'jlo') #daj mi pratioce od datog user-a
    print(profile.get_posts())
    
    for post in profile.get_posts():
    
        post_likes = post.get_likes()
        post_comments = post.get_comments()
    
        print(post_likes)  # post_likes object
        print(post_comments) # # post_comments object
    
        # Iterate over all likes of the post. A Profile instance of each likee is yielded.
        for likee in post_likes:
            print(likee.username)
    
        # Iterate over all comments of the post.
        # Each comment is represented by a PostComment namedtuple with fields 
        # text (string), created_at (datetime), id (int), owner (Profile) 
        # and answers (~typing.Iterator[PostCommentAnswer]) if available.
        for comment in post_comments:
            print(comment.owner.username)