Why can't I get all Instagram posts without using API in NodeJS

Viewed 1288

Notice as of today March 2020, Instagram has changed considerably, hence, this question is not similar to previous ones. The issue is that I always get 12 posts instead of all Posts using non Instagram-API ways. I have came across many solutions but all of them only return 12 posts now, even the famous:

https://www.instagram.com/instagram/?__a=1

This is what I get with instagram account that has 6282 posts:

enter image description here

I have also tried the following trick, but it also returns 12 posts:

async function instagramPhotos () {
    // It will contain our photos' links
    const res = []
    
    try {
        const userInfoSource = await Axios.get('https://www.instagram.com/instagram/')

        // userInfoSource.data contains the HTML from Axios
        const jsonObject = userInfoSource.data.match(/<script type="text\/javascript">window\._sharedData = (.*)<\/script>/)[1].slice(0, -1)

        const userInfo = JSON.parse(jsonObject)
        // Retrieve only the first 10 results
        const mediaArray = userInfo.entry_data.ProfilePage[0].graphql.user.edge_owner_to_timeline_media.edges
        for (let media of mediaArray) {
            const node = media.node
            
            // Process only if is an image
            if ((node.__typename && node.__typename !== 'GraphImage')) {
                continue
            }

            // Push the thumbnail src in the array
            res.push(node.thumbnail_src)
        }
    } catch (e) {
        console.error('Unable to retrieve photos. Reason: ' + e.toString())
    }
    
    return res
}

There are many npm modules that claim they can get feed without using Instagram API but at the end they only return 12 posts, for example: (instagram-nodejs-without-api)

I run the following code, and I also get only 12 posts:

let Instagram = require('instagram-nodejs-without-api');
Instagram = new Instagram()

const username="instagram"
const password="123hola" 

Instagram.getCsrfToken().then((csrf) =>
{
  Instagram.csrfToken = csrf;
}).then(() =>
{
  return Instagram.auth(username, password).then(sessionId =>
  {
    Instagram.sessionId = sessionId

    return Instagram.getUserDataByUsername(username).then((t) =>
    {
        console.log(t)
    })

  })
}).catch(console.error);

My next step is to perform web scraping over Instagram profile but it is very slow specially when images are around 6000, Is there any elegant approach to get these posts? I only want to get my posts so I don't mind to be logged In.

1 Answers

Since I needed to get posts from a concrete profile, I opened the profile in chrome browser, opened the console and runned the following code to get all posts:

//LOOPING ALL (credit www.hamzadiaz.com)
let posts = []
setInterval(()=>{
var inputs = document.getElementsByClassName('v1Nh3 kIKUG  _bz0w'); 
for(var i=0; i<inputs.length;i++) {
 if(!posts.includes(inputs[i].getElementsByTagName("a")[0].href)){
    posts.push(inputs[i].getElementsByTagName("a")[0].href)
  }
}
},500);
Related