Search code examples
instagraminstagram-api

Is there a way to get the follower list of an instagram account?


I noticed instagram changed their API but I would like to clean up my followers (I noticed some of them are bots and that just looks bad).

I would like to get a list of the usernames so I can determine if I want to block them or not (manually, because of API changes).

Is there any way that I could download that information?

I've tried different websites but they either don't work anymore, ask for money (and probably wouldn't work) or they just vanished.

I've also tried some python scripts with instaloader and they all retreive nothing, as in they login just fine, but can't show me any information.


Solution

  • There is a way using javascript and Instagram's unofficial API.

    Here are two functions I have written:

    The first is random_wait_time to wait between the requests, the second one get_followers makes the actual requests.

    const random_wait_time = (waitTime = 300) => new Promise((resolve, reject) => {
      setTimeout(() => {
        return resolve();
      }, Math.random() * waitTime);
    });
    
    
    const get_followers = async(userId, userFollowerCount) => {
      let userFollowers = [],
        batchCount = 20,
        actuallyFetched = 20,
        url = `https://www.instagram.com/graphql/query/?query_hash=c76146de99bb02f6415203be841dd25a&variables={"id":"${userId}","include_reel":true,"fetch_mutual":true,"first":"${batchCount}"}`;
      while (userFollowerCount > 0) {
        const followersResponse = await fetch(url)
          .then(res => res.json())
          .then(res => {
            const nodeIds = [];
            for (const node of res.data.user.edge_followed_by.edges) {
              nodeIds.push(node.node.id);
            }
            actuallyFetched = nodeIds.length;
            return {
              edges: nodeIds,
              endCursor: res.data.user.edge_followed_by.page_info.end_cursor
            };
          }).catch(err => {
            userFollowerCount = -1;
            return {
              edges: []
            };
          });
        await random_wait_time();
        userFollowers = [...userFollowers, ...followersResponse.edges];
        userFollowerCount -= actuallyFetched;
        url = `https://www.instagram.com/graphql/query/?query_hash=c76146de99bb02f6415203be841dd25a&variables={"id":"${userId}","include_reel":true,"fetch_mutual":true,"first":${batchCount},"after":"${followersResponse.endCursor}"}`;
      }
      console.log(userFollowers);
      return userFollowers;
    };

    Now open the developer console of your browser with F12on Windows or Cmd + Alt + j on Mac and paste the code from above.

    Then proceed to call get_followers with your user_id and followers_count or an upper limit:

    get_followers(493525227, 10)

    and copy the result user_id of your followers.

    If you want all the data for the followers then change the returned object:

    return { edges: res.data.user.edge_followed_by.edges, endCursor: res.data.user.edge_followed_by.page_info.end_cursor };

    P.S To get your user_id you can use that service or the value of the cookie ds_user_id in the developer console.

    The reason to paste the code in the console is that each request automatically passes all the available cookies on the domain. Doing so not from instagram.com requires you to supply all the cookies manually.

    Hope it helps !