How to get followers and following list in Instagram via http requests

后端 未结 2 2026
后悔当初
后悔当初 2021-02-07 10:46

I\'m looking for a possibility to get a followers and following list in JSON format via web request (in the same way as on the Instagram web site). For example, I can login via

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-07 11:15

    GraphQL queries with query_hash = "58712303d941c6855d4e888c5f0cd22f" (followings) and "37479f2b8209594dde7facb0d904896a" (followers) return this information. With being logged in, do a GET query to instagram.com/graphql/query with parameters query_hash and variables, where variables is a JSON-formatted set of variables id (user id, as in the return dictionary of your get_user_info() function), first (a page length, it seems the current maximum is 50) and in subsequent requests after set to the end_cursor in the previous response dictionary.

    Alternatively, the Instaloader library provides a convenient way to login and then programmatically access a profile's followers and followings list.

    import instaloader
    
    # Get instance
    L = instaloader.Instaloader()
    
    # Login or load session
    L.login(USER, PASSWORD)        # (login)
    L.interactive_login(USER)      # (ask password on terminal)
    L.load_session_from_file(USER) # (load session created w/
                                   #  `instaloader -l USERNAME`)
    
    # Obtain profile metadata
    profile = instaloader.Profile.from_username(L.context, PROFILE)
    
    # Print list of followees
    for followee in profile.get_followees():
        print(followee.username)
    
    # (likewise with profile.get_followers())
    

    Besides username, the attributes full_name, userid, followed_by_viewer and many more are defined in the Profile instance that is returned for each followee.

提交回复
热议问题