14

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?

Baker
  • 161
  • 1
  • 1
  • 13

3 Answers3

49

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.

ilyapt
  • 1,829
  • 17
  • 12
  • Any idea what the best way to do this is? GET? – Baker Oct 04 '16 at 01:43
  • 1
    @Baker Yep, get by php curl etc – ilyapt Oct 04 '16 at 01:53
  • This no longer seems to be working for me as of May 2018, but I'm still able to get the counts for followers, following, and posts by parsing the raw html from the regular profile url `https://www.instagram.com/` – LulzCow May 01 '18 at 19:40
  • This works when i open it in a browser, but when i download with code it comes out as an HTML page. And parsing it via the HTML of the profile page seems to have broken a few days ago as well. – stackers Oct 17 '19 at 21:36
  • 5
    "instagram.com/$username/?__a=1" solution works but after some weeks, instagram blocks that domain thus system does not able to fetch followers count to display on our website. I faced this issue on 2 domains/websites. – Kamlesh Jun 21 '20 at 04:23
  • 1
    yes, instagram blocks the URL and starts giving a 429 (too many request) error after some time – Kumar Ravi Feb 22 '21 at 11:52
  • 1
    It's not woking when I called it from application layer, e.g., Java – Hikaru Shindo Mar 18 '21 at 06:51
6

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.

LulzCow
  • 1,199
  • 2
  • 9
  • 21
0

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?

ozgrozer
  • 1,824
  • 1
  • 23
  • 35