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

32,644

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.

Share:
32,644

Related videos on Youtube

Ilya Kryukov
Author by

Ilya Kryukov

Updated on September 16, 2020

Comments

  • Ilya Kryukov
    Ilya Kryukov over 3 years

    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 requests, and get user info:

    def get_user_info(self, user_name):
        url = "https://www.instagram.com/" + user_name + "/?__a=1"
        try:
            r = requests.get(url)
        except requests.exceptions.ConnectionError:
            print 'Seems like dns lookup failed..'
            time.sleep(60)
            return None
        if r.status_code != 200:
            print 'User: ' + user_name + ' status code: ' + str(r.status_code)
            print r
            return None
        info = json.loads(r.text)
        return info['user']
    

    I tried to see what request chrome send to server, but was unsuccessful. The question is: how to prepare a similar get or post request to retrieve followers list without the Instagram API?

  • Ilya Kryukov
    Ilya Kryukov almost 8 years
    I tried this approach, but it redirect on user_name main page, but how to get followers list?
  • Ilya Kryukov
    Ilya Kryukov almost 8 years
    I tried on public account. Does it work in your case?
  • Вадим Виноградов
    Вадим Виноградов almost 8 years
    Yeah, it's work in browser, with requests.get or with curl
  • Ilya Kryukov
    Ilya Kryukov almost 8 years
    Could you attach screenshot from browser here for any public account?
  • Torsten Ojaperv
    Torsten Ojaperv over 7 years
    Disabled for sure. Will prompt login.
  • chrisTina
    chrisTina about 5 years
    Seems like a lot of similar tool is using GraphQL with query_hash for followers. I am trying to approach the same thing use Java. Do you think it is applicable? Do you have any code to share? Thanks a lot.
  • Chiefir
    Chiefir over 4 years
    @aandergr is that ok that every 1k scraped followers i get blocked to ~10 minutes cuz of: Too many queries in the last time. Need to wait 630 seconds, until ... Can I somehow pass through that?

Related