Baker
Baker

Reputation: 161

Grab Instagram Follower count

So I have a question what would be the method to just grab the instagram follower count for a said user?

I have looked at two possible options the official instagram API, but I couldn't find a specific method named on how to do so, but they do have a some user endpoints, but couldn't find much detail on it or I was thinking about using the unofficial instagram API https://github.com/mgp25/Instagram-API

Any tips?

Upvotes: 14

Views: 29113

Answers (3)

ozgrozer
ozgrozer

Reputation: 2042

When you make a GET request to any Instagram profile and try to get the HTML you'd see something like window._sharedData = "..." in the code.

enter image description here

Then you basically get the content of that variable and convert to an object to access any data in it. And you console log json.entry_data.ProfilePage[0].graphql.user, you'll get all the informations about the user.

Here's an example with Axios.

const axios = require('axios')

const run = async () => {
  try {
    const post = await axios({
      method: 'get',
      url: 'https://www.instagram.com/ozgrozer/'
    })

    const pattern = /<script type="text\/javascript">window._sharedData = ([\s\S]*?);<\/script>/gi
    const matches = pattern.exec(post.data)
    const scriptContent = matches[1]
    const json = JSON.parse(scriptContent)

    if (json.entry_data.LoginAndSignupPage) throw new Error('Login required')

    console.log(json.entry_data.ProfilePage[0].graphql.user)
    /*
      {
        "id": "7851687561",
        "full_name": "Ozgur Ozer",
        "edge_follow": { "count": 87 },
        "biography": "American oxygen 🇺🇸",
        "edge_followed_by": { "count": 102 },
        "external_url": "http://about.me/ozgrozer",
      }
    */
  } catch (err) {
    console.log(err)
  }
}

run()

Update: After a couple of running the code above I realized Instagram started to return something LoginAndSignupPage: [] in the json.entry_data and basically blocking the requests. Not sure what to do right now. Maybe connecting with a proxy or Tor?

Upvotes: 0

LulzCow
LulzCow

Reputation: 1229

The link from the accepted answer (https://www.instagram.com/<username>/?__a=1) no longer seems to work, but we can still get followers count from parsing the html from the normal profile url https://www.instagram.com/<username>

If you do a GET request, you'll get the plain HTML and you can search an html tag that looks like <link rel="canonical" href="https://www.instagram.com/<username>/" /><meta content="359 Followers, 903 Following, 32 Posts - See Instagram photos and videos from <username>)" name="description" />

You can try it out in your browser by going to an Instagram profile, then right click and viewing the page source. Then it's just a matter of parsing the text to get the info you want.

Here's an example to get the number of followers in javascript:

var url = "https://www.instagram.com/username";
request.get(url, function(err, response, body){
    if(response.body.indexOf(("meta property=\"og:description\" content=\"")) != -1){
        console.log("followers:", response.body.split("meta property=\"og:description\" content=\"")[1].split("Followers")[0])
    }
 });

This is probably not a reliable, future-proof approach, but it does seem to work for now.

Upvotes: 6

ilyapt
ilyapt

Reputation: 1829

You can request https://www.instagram.com/<username>/?__a=1 and receive JSON with account information also with followers count as well. It doesn't need authorization.

Upvotes: 49

Related Questions