How can I get a user's media from Instagram without authenticating as a user?

Instagram Api

Instagram Api Problem Overview


I'm trying to put a user's recent Instagram media on a sidebar. I'm trying to use the Instagram API to fetch the media.

http://instagram.com/developer/endpoints/users/

The documentation says to GET https://api.instagram.com/v1/users/<user-id>/media/recent/, but it says to pass an OAuth access token. An access token represents the authorization to act on behalf of a user. I don't want users to log into Instagram to see this on the sidebar. They shouldn't even need to have an Instagram account.

For instance, I can go to http://instagram.com/thebrainscoop without being logged into Instagram and see photos. I want to do that through the API.

In the Instagram API, non-user-authenticated requests pass a client_id instead of an access_token. If I try that, though, I get:

{
  "meta":{
    "error_type":"OAuthParameterException",
    "code":400,
    "error_message":"\"access_token\" URL parameter missing. This OAuth request requires an \"access_token\" URL parameter."
  }
}

So, is this not possible? Is there no way to fetch a user's latest (public) media without asking a user to log into an Instagram account through OAuth first?

Instagram Api Solutions


Solution 1 - Instagram Api

var name = "smena8m";
$.get("https://images"+~~(Math.random()*3333)+"-focus-opensocial.googleusercontent.com/gadgets/proxy?container=none&url=https://www.instagram.com/" + name + "/", function(html) {
if (html) {
    var regex = /_sharedData = ({.*);<\/script>/m,
        json = JSON.parse(regex.exec(html)[1]),
        edges = json.entry_data.ProfilePage[0].graphql.user.edge_owner_to_timeline_media.edges;

      $.each(edges, function(n, edge) {
          var node = edge.node;
          $('body').append(
              $('<a/>', {
              href: 'https://instagr.am/p/'+node.shortcode,
              target: '_blank'
          }).css({
              backgroundImage: 'url(' + node.thumbnail_src + ')'
          }));
      });
    }
});

html, body {
  font-size: 0;
  line-height: 0;
}

a {
  display: inline-block;
  width: 25%;
  height: 0;
  padding-bottom: 25%;
  background: #eee 50% 50% no-repeat;
  background-size: cover;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

You can download any Instagram user photo feed in JSON format using ?__a=1 next to landing page address like this. No need to get user id or register an app, no tokens, no oAuth.

min_id and max_id variables can be used for pagination, here is example

YQL may not work here inside snipped iframe, so you can always check it manually in YQL Console

APRIL 2018 UPDATE: After latest instagram updates you can't do this on client side (javascript) because custom headers for signed request can't be set with javascript due to CORS Access-Control-Allow-Headers restrictions. It still possible to do this via php or any other server side method with proper signature based on rhx_gis, csrf_token and request parameters. You can read more about it here.

JANUARY 2019 UPDATE: YQL retired, so, check my latest update with Google Image Proxy as CORS proxy for Instagram page! Then only negative moment - pagination not available with this method.

PHP solution:

    $html = file_get_contents('https://instagram.com/apple/');
    preg_match('/_sharedData = ({.*);<\/script>/', $html, $matches);
    $profile_data = json_decode($matches[1])->entry_data->ProfilePage[0]->graphql->user;

Solution 2 - Instagram Api

This is late, but worthwhile if it helps someone as I did not see it in Instagram's documentation.

To perform GET on https://api.instagram.com/v1/users/<user-id>/media/recent/ (at present time of writing) you actually do not need OAuth access token.

You can perform https://api.instagram.com/v1/users/[USER ID]/media/recent/?client_id=[CLIENT ID]

[CLIENT ID] would be valid client id registered in app through manage clients (not related to user whatsoever). You can get [USER ID] from username by performing GET users search request: https://api.instagram.com/v1/users/search?q=[USERNAME]&client_id=[CLIENT ID]

Solution 3 - Instagram Api

11.11.2017
Since Instagram changed the way they provide this data, none of above methods work nowadays. Here is the new way to get user's media:
GET https://instagram.com/graphql/query/?query_id=17888483320059182&variables={"id":"1951415043","first":20,"after":null}
Where:
query_id - permanent value: 17888483320059182 (note it might be changed in future).
id - id of the user. It may come with list of users. To get the list of users you can use following request: GET https://www.instagram.com/web/search/topsearch/?context=blended&query=YOUR_QUERY
first - amount of items to get.
after - id of the last item if you want to get items from that id.

Solution 4 - Instagram Api

I was able to get the most recent media of a user using the following API without authentication (including the description, likes, comments count).

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

E.g.

https://www.instagram.com/{username}/?__a=1

Solution 5 - Instagram Api

As of last week, Instagram disabled /media/ urls, I implemented a workaround, which works pretty well for now.

To solve everyone's problems in this thread, I wrote this: https://github.com/whizzzkid/instagram-reverse-proxy

It provides all of instagram's public data using the following endpoints:

Get user media:

https://igapi.ga/<username>/media
e.g.: https://igapi.ga/whizzzkid/media 

Get user media with limit count:

https://igapi.ga/<username>/media?count=N // 1 < N < 20
e.g.: https://igapi.ga/whizzzkid/media?count=5

Use JSONP:

https://igapi.ga/<username>/media?callback=foo
e.g.: https://igapi.ga/whizzzkid/media?callback=bar

The proxy API also appends next page and previous page URLs to the response so you do not need to calculate that at your end.

Hope you guys like it!

Thanks to @350D for spotting this :)

Solution 6 - Instagram Api

The Instagram API requires user authentication through OAuth to access the recent media endpoint for a user. There doesn't appear to be any other way right now to get all media for a user.

Solution 7 - Instagram Api

Here's a rails solutions. It's kind of back-door, which is actually the front door.

# create a headless browser
b = Watir::Browser.new :phantomjs
uri = 'https://www.instagram.com/explore/tags/' + query
uri = 'https://www.instagram.com/' + query if type == 'user'

b.goto uri

# all data are stored on this page-level object.
o = b.execute_script( 'return window._sharedData;')

b.close

The object you get back varies depending on whether or not it's a user search or a tag search. I get the data like this:

if type == 'user'
  data = o[ 'entry_data' ][ 'ProfilePage' ][ 0 ][ 'user' ][ 'media' ][ 'nodes' ]
  page_info = o[ 'entry_data' ][ 'ProfilePage' ][ 0 ][ 'user' ][ 'media' ][ 'page_info' ]
  max_id = page_info[ 'end_cursor' ]
  has_next_page = page_info[ 'has_next_page' ]
else
  data = o[ 'entry_data' ][ 'TagPage' ][ 0 ][ 'tag' ][ 'media' ][ 'nodes' ]
  page_info = o[ 'entry_data' ][ 'TagPage' ][ 0 ][ 'tag' ][ 'media' ][ 'page_info' ]
  max_id = page_info[ 'end_cursor' ]
  has_next_page = page_info[ 'has_next_page' ]
end

I then get another page of results by constructing a url in the following way:

  uri = 'https://www.instagram.com/explore/tags/' + query_string.to_s\
    + '?&max_id=' + max_id.to_s
  uri = 'https://www.instagram.com/' + query_string.to_s + '?&max_id='\
    + max_id.to_s if type === 'user'

Solution 8 - Instagram Api

Thanks to Instagram's ever changing (and horrifically designed) API schema most of the above will no longer work as of April 2018.

Here is the latest path to access individual post data if you are querying their API directly using the https://www.instagram.com/username/?__a=1 method.

Assuming your returned JSON data is $data you can loop through each result using the following path examples :

foreach ($data->graphql->user->edge_owner_to_timeline_media->edges as $item) {

    $content_id = $item->node->id; 
    $date_posted = $item-node->taken_at_timestamp;
    $comments = $item->node->edge_media_to_comment->count;
    $likes = $item->node->edge_liked_by->count;
    $image = $item->node->display_url;
    $content = $item->node->edge_media_to_caption->edges[0]->node->text;
    // etc etc ....
}

The main things in this recent change were graphql and edge_owner_to_timeline_media.

Looks like they are going to be killing this API access off for non 'business' customers in DEC 2018 so make the most of it while you can.

Hope it helps somebody ;)

Solution 9 - Instagram Api

If you are looking for a way to generate an access token for use on a single account, you can try this -> https://coderwall.com/p/cfgneq.

I needed a way to use the instagram api to grab all the latest media for a particular account.

Solution 10 - Instagram Api

Just want to add to @350D answer, since it was hard for me to understand.

My logic in code is next:

When calling API first time, i'm calling only https://www.instagram.com/_vull_ /media/. When I receive response, I check boolean value of more_available. If its true, I get the last photo from the array, get its id and then call Instagram API again but this time https://www.instagram.com/_vull_/media/?max_id=1400286183132701451_1642962433.

Important thing to know here, this Id is the Id of the last picture in the array. So when asking for maxId with the last id of the picture in the array, you will get next 20 pictures, and so on.

Hope this clarify things.

Solution 11 - Instagram Api

JSFiddle

Javascript:

$(document).ready(function(){

    var username = "leomessi";
    var max_num_items = 5;

    var jqxhr = $.ajax( "https://www.instagram.com/"+username+"/?__a=1" ).done(function() {
	    //alert( "success" );
    }).fail(function() {
	    //alert( "error" );
    }).always(function(data) {
	    //alert( "complete" )
        items = data.graphql.user.edge_owner_to_timeline_media.edges;
	    $.each(items, function(n, item) {
		    if( (n+1) <= max_num_items )
		    {
			    var data_li = "<li><a target='_blank' href='https://www.instagram.com/p/"+item.node.shortcode+"'><img src='" + item.node.thumbnail_src + "'/></a></li>";
			    $("ul.instagram").append(data_li);
		    }
	    });

    });

});

HTML:

<ul class="instagram">
</ul>

CSS:

ul.instagram {
    list-style: none;
}

ul.instagram li {
  float: left;
}

ul.instagram li img {
    height: 100px;
}

Solution 12 - Instagram Api

One more trick, search photos by hashtags:

GET https://www.instagram.com/graphql/query/?query_hash=3e7706b09c6184d5eafd8b032dbcf487&variables={"tag_name":"nature","first":25,"after":""}

Where:

query_hash - permanent value(i belive its hash of 17888483320059182, can be changed in future)

tag_name - the title speaks for itself

first - amount of items to get (I do not know why, but this value does not work as expected. The actual number of returned photos is slightly larger than the value multiplied by 4.5 (about 110 for the value 25, and about 460 for the value 100))

after - id of the last item if you want to get items from that id. Value of end_cursor from JSON response can be used here.

Solution 13 - Instagram Api

If you bypass Oauth you probably wouldn't know which instagram user they are. That being said there are a few ways to get instagram images without authentication.

  1. Instagram's API allows you to view a user's most popular images without authenticating. Using the following endpoint: Here is link

  2. Instagram provides rss feeds for tags at this.

  3. Instagram user pages are public, so you can use PHP with CURL to get their page and a DOM parser to search the html for the image tags you want.

Solution 14 - Instagram Api

If you want to search users without having clientID and access token:

1: If you want to search all users having your names similar to your search word :

replace SeachName with text you want to search:

https://www.instagram.com/web/search/topsearch/?query=SearchName

2: if you want to search exact same name user :

replace UserName with your desired search Name:

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

Solution 15 - Instagram Api

Well, as /?__a=1 stopped working by now, it's better to use curl and parse the instagram page as written at this answer: Generate access token Instagram API, without having to log in?

Solution 16 - Instagram Api

You can use this API to retrieve public info of the instagram user:

https://api.lityapp.com/instagrams/thebrainscoop?limit=2 (edit: broken/malware link on Feb 2021)

If you don't set the limit parameter, the posts are limited at 12 by default

This api was made in SpringBoot with HtmlUnit as you can see in the code:

    public JSONObject getPublicInstagramByUserName(String userName, Integer limit) {
        String html;
        WebClient webClient = new WebClient();
    
        try {
            webClient.getOptions().setCssEnabled(false);
            webClient.getOptions().setJavaScriptEnabled(false);
            webClient.getOptions().setThrowExceptionOnScriptError(false);
            webClient.getCookieManager().setCookiesEnabled(true);
    
            Page page = webClient.getPage("https://www.instagram.com/" + userName);
            WebResponse response = page.getWebResponse();
    
            html = response.getContentAsString();
        } catch (Exception ex) {
            ex.printStackTrace();
    
            throw new RuntimeException("Ocorreu um erro no Instagram");
        }
    
        String prefix = "static/bundles/es6/ProfilePageContainer.js";
        String suffix = "\"";
        String script = html.substring(html.indexOf(prefix));
    
        script = script.substring(0, script.indexOf(suffix));
    
        try {
            Page page = webClient.getPage("https://www.instagram.com/" + script);
            WebResponse response = page.getWebResponse();
    
            script = response.getContentAsString();
        } catch (Exception ex) {
            ex.printStackTrace();
    
            throw new RuntimeException("Ocorreu um erro no Instagram");
        }
    
        prefix = "l.pagination},queryId:\"";
    
        String queryHash = script.substring(script.indexOf(prefix) + prefix.length());
    
        queryHash = queryHash.substring(0, queryHash.indexOf(suffix));
        prefix = "<script type=\"text/javascript\">window._sharedData = ";
        suffix = ";</script>";
        html = html.substring(html.indexOf(prefix) + prefix.length());
        html = html.substring(0, html.indexOf(suffix));
    
        JSONObject json = new JSONObject(html);
        JSONObject entryData = json.getJSONObject("entry_data");
        JSONObject profilePage = (JSONObject) entryData.getJSONArray("ProfilePage").get(0);
        JSONObject graphql = profilePage.getJSONObject("graphql");
        JSONObject user = graphql.getJSONObject("user");
        JSONObject response = new JSONObject();
    
        response.put("id", user.getString("id"));
        response.put("username", user.getString("username"));
        response.put("fullName", user.getString("full_name"));
        response.put("followedBy", user.getJSONObject("edge_followed_by").getLong("count"));
        response.put("following", user.getJSONObject("edge_follow").getLong("count"));
        response.put("isBusinessAccount", user.getBoolean("is_business_account"));
        response.put("photoUrl", user.getString("profile_pic_url"));
        response.put("photoUrlHD", user.getString("profile_pic_url_hd"));
    
        JSONObject edgeOwnerToTimelineMedia = user.getJSONObject("edge_owner_to_timeline_media");
        JSONArray posts = new JSONArray();
    
        try {
            loadPublicInstagramPosts(webClient, queryHash, user.getString("id"), posts, edgeOwnerToTimelineMedia, limit == null ? 12 : limit);
        } catch (Exception ex) {
            ex.printStackTrace();
    
            throw new RuntimeException("VocĂȘ fez muitas chamadas, tente mais tarde");
        }
    
        response.put("posts", posts);
    
        return response;
    }
    
    private void loadPublicInstagramPosts(WebClient webClient, String queryHash, String userId, JSONArray posts, JSONObject edgeOwnerToTimelineMedia, Integer limit) throws IOException {
        JSONArray edges = edgeOwnerToTimelineMedia.getJSONArray("edges");
    
        for (Object elem : edges) {
            if (limit != null && posts.length() == limit) {
                return;
            }
    
            JSONObject node = ((JSONObject) elem).getJSONObject("node");
    
            if (node.getBoolean("is_video")) {
                continue;
            }
    
            JSONObject post = new JSONObject();
    
            post.put("id", node.getString("id"));
            post.put("shortcode", node.getString("shortcode"));
    
            JSONArray captionEdges = node.getJSONObject("edge_media_to_caption").getJSONArray("edges");
    
            if (captionEdges.length() > 0) {
                JSONObject captionNode = ((JSONObject) captionEdges.get(0)).getJSONObject("node");
    
                post.put("caption", captionNode.getString("text"));
            } else {
                post.put("caption", (Object) null);
            }
    
            post.put("photoUrl", node.getString("display_url"));
    
            JSONObject dimensions = node.getJSONObject("dimensions");
    
            post.put("photoWidth", dimensions.getLong("width"));
            post.put("photoHeight", dimensions.getLong("height"));
    
            JSONArray thumbnailResources = node.getJSONArray("thumbnail_resources");
            JSONArray thumbnails = new JSONArray();
    
            for (Object elem2 : thumbnailResources) {
                JSONObject obj = (JSONObject) elem2;
                JSONObject thumbnail = new JSONObject();
    
                thumbnail.put("photoUrl", obj.getString("src"));
                thumbnail.put("photoWidth", obj.getLong("config_width"));
                thumbnail.put("photoHeight", obj.getLong("config_height"));
                thumbnails.put(thumbnail);
            }
    
            post.put("thumbnails", thumbnails);
            posts.put(post);
        }
    
        JSONObject pageInfo = edgeOwnerToTimelineMedia.getJSONObject("page_info");
    
        if (!pageInfo.getBoolean("has_next_page")) {
            return;
        }
    
        String endCursor = pageInfo.getString("end_cursor");
        String variables = "{\"id\":\"" + userId + "\",\"first\":12,\"after\":\"" + endCursor + "\"}";
    
        String url = "https://www.instagram.com/graphql/query/?query_hash=" + queryHash + "&variables=" + URLEncoder.encode(variables, "UTF-8");
        Page page = webClient.getPage(url);
        WebResponse response = page.getWebResponse();
        String content = response.getContentAsString();
        JSONObject json = new JSONObject(content);
    
        loadPublicInstagramPosts(webClient, queryHash, userId, posts, json.getJSONObject("data").getJSONObject("user").getJSONObject("edge_owner_to_timeline_media"), limit);
    }

It's an example of response:

    {
      "id": "290482318",
      "username": "thebrainscoop",
      "fullName": "Official Fan Page",
      "followedBy": 1023,
      "following": 6,
      "isBusinessAccount": false,
      "photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/447ffd0262082f373acf3d467435f130/5C709C77/t51.2885-19/11351770_612904665516559_678168252_a.jpg",
      "photoUrlHD": "https://scontent-gru2-1.cdninstagram.com/vp/447ffd0262082f373acf3d467435f130/5C709C77/t51.2885-19/11351770_612904665516559_678168252_a.jpg",
      "posts": [
        {
          "id": "1430331382090378714",
          "shortcode": "BPZjtBUly3a",
          "caption": "If I have any active followers anymore; hello! I'm Brianna, and I created this account when I was just 12 years old to show my love for The Brain Scoop. I'm now nearly finished high school, and just rediscovered it. I just wanted to see if anyone is still active on here, and also correct some of my past mistakes - being a child at the time, I didn't realise I had to credit artists for their work, so I'm going to try to correct that post haste. Also; the font in my bio is horrendous. Why'd I think that was a good idea? Anyway, this is a beautiful artwork of the long-tailed pangolin by @chelsealinaeve . Check her out!",
          "photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/ab823331376ca46136457f4654bf2880/5CAD48E4/t51.2885-15/e35/16110915_400942200241213_3503127351280009216_n.jpg",
          "photoWidth": 640,
          "photoHeight": 457,
          "thumbnails": [
            {
              "photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/43b195566d0ef2ad5f4663ff76d62d23/5C76D756/t51.2885-15/e35/c91.0.457.457/s150x150/16110915_400942200241213_3503127351280009216_n.jpg",
              "photoWidth": 150,
              "photoHeight": 150
            },
            {
              "photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/ae39043a7ac050c56d741d8b4355c185/5C93971C/t51.2885-15/e35/c91.0.457.457/s240x240/16110915_400942200241213_3503127351280009216_n.jpg",
              "photoWidth": 240,
              "photoHeight": 240
            },
            {
              "photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/ae7a22d09e3ef98d0a6bbf31d621a3b7/5CACBBA6/t51.2885-15/e35/c91.0.457.457/s320x320/16110915_400942200241213_3503127351280009216_n.jpg",
              "photoWidth": 320,
              "photoHeight": 320
            },
            {
              "photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/1439dc72b70e7c0c0a3afcc30970bb13/5C8E2923/t51.2885-15/e35/c91.0.457.457/16110915_400942200241213_3503127351280009216_n.jpg",
              "photoWidth": 480,
              "photoHeight": 480
            },
            {
              "photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/1439dc72b70e7c0c0a3afcc30970bb13/5C8E2923/t51.2885-15/e35/c91.0.457.457/16110915_400942200241213_3503127351280009216_n.jpg",
              "photoWidth": 640,
              "photoHeight": 640
            }
          ]
        },
        {
          "id": "442527661838057235",
          "shortcode": "YkLJBXJD8T",
          "caption": null,
          "photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/dc94b38da679826b9ac94ccd2bcc4928/5C7CDF93/t51.2885-15/e15/11327349_860747310663863_2105199307_n.jpg",
          "photoWidth": 612,
          "photoHeight": 612,
          "thumbnails": [
            {
              "photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/c1153c6513c44a6463d897e14b2d8f06/5CB13ADD/t51.2885-15/e15/s150x150/11327349_860747310663863_2105199307_n.jpg",
              "photoWidth": 150,
              "photoHeight": 150
            },
            {
              "photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/47e60ec8bca5a1382cd9ac562439d48c/5CAE6A82/t51.2885-15/e15/s240x240/11327349_860747310663863_2105199307_n.jpg",
              "photoWidth": 240,
              "photoHeight": 240
            },
            {
              "photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/da0ee5b666ab40e4adc1119e2edca014/5CADCB59/t51.2885-15/e15/s320x320/11327349_860747310663863_2105199307_n.jpg",
              "photoWidth": 320,
              "photoHeight": 320
            },
            {
              "photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/02ee23571322ea8d0992e81e72f80ef2/5C741048/t51.2885-15/e15/s480x480/11327349_860747310663863_2105199307_n.jpg",
              "photoWidth": 480,
              "photoHeight": 480
            },
            {
              "photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/dc94b38da679826b9ac94ccd2bcc4928/5C7CDF93/t51.2885-15/e15/11327349_860747310663863_2105199307_n.jpg",
              "photoWidth": 640,
              "photoHeight": 640
            }
          ]
        }
      ]
    }

Solution 17 - Instagram Api

I really needed this function but for Wordpress. I fit and it worked perfectly

<script>
	jQuery(function($){
		var name = "caririceara.comcariri";
		$.get("https://images"+~~(Math.random()*33)+"-focus-opensocial.googleusercontent.com/gadgets/proxy?container=none&url=https://www.instagram.com/" + name + "/", function(html) {
			if (html) {
				var regex = /_sharedData = ({.*);<\/script>/m,
				  json = JSON.parse(regex.exec(html)[1]),
				  edges = json.entry_data.ProfilePage[0].graphql.user.edge_owner_to_timeline_media.edges;
			  $.each(edges, function(n, edge) {
				   if (n <= 7){
					 var node = edge.node;
					$('.img_ins').append('<a href="https://instagr.am/p/'+node.shortcode+'" target="_blank"><img src="'+node.thumbnail_src+'" width="150"></a>');
				   }
			  });
			}
		});
	});	
	</script>

Solution 18 - Instagram Api

The below nodejs code scrapes popular Images from an Instagram Page. The function 'ScrapeInstagramPage' takes care of post ageing effect.

var request = require('parse5');
var request = require('request');
var rp      = require('request-promise');
var $       = require('cheerio'); // Basically jQuery for node.js 
const jsdom = require("jsdom");    
const { JSDOM } = jsdom;
    
    
function ScrapeInstagramPage (args) {
    dout("ScrapeInstagramPage for username -> " + args.username);
    var query_url = 'https://www.instagram.com/' + args.username + '/';

    var cookieString = '';

    var options = {
        url: query_url,
        method: 'GET',
        headers: {
            'x-requested-with' : 'XMLHttpRequest',
            'accept-language'  : 'en-US,en;q=0.8,pt;q=0.6,hi;q=0.4', 
            'User-Agent'       : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
            'referer'          : 'https://www.instagram.com/dress_blouse_designer/',
            'Cookie'           : cookieString,
            'Accept'           : '*/*',
            'Connection'       : 'keep-alive',
            'authority'        : 'www.instagram.com' 
        }
    };


    function dout (msg) {
        if (args.debug) {
            console.log(msg);
        }
    }
    
    function autoParse(body, response, resolveWithFullResponse) {
        // FIXME: The content type string could contain additional values like the charset. 
        // Consider using the `content-type` library for a robust comparison. 
        if (response.headers['content-type'] === 'application/json') {
            return JSON.parse(body);
        } else if (response.headers['content-type'] === 'text/html') {
            return $.load(body);
        } else {
            return body;
        }
    }

    options.transform = autoParse;


    rp(options)
        .then(function (autoParsedBody) {
            if (args.debug) {
                console.log("Responce of 'Get first user page': ");
                console.log(autoParsedBody);
                console.log("Creating JSDOM from above Responce...");
            }

            const dom = new JSDOM(autoParsedBody.html(), { runScripts: "dangerously" });
            if (args.debug) console.log(dom.window._sharedData); // full data doc form instagram for a page

            var user = dom.window._sharedData.entry_data.ProfilePage[0].user;
            if (args.debug) {
                console.log(user); // page user
                console.log(user.id); // user ID
                console.log(user.full_name); // user full_name
                console.log(user.username); // user username
                console.log(user.followed_by.count); // user followed_by
                console.log(user.profile_pic_url_hd); // user profile pic
                console.log(autoParsedBody.html());
            }
            
            if (user.is_private) {
                dout ("User account is PRIVATE");
            } else {
                dout ("User account is public");
                GetPostsFromUser(user.id, 5000, undefined);
            }
        })
        .catch(function (err) {
            console.log( "ERROR: " + err );
        });  

    var pop_posts = [];
    function GetPostsFromUser (user_id, first, end_cursor) {
        var end_cursor_str = "";
        if (end_cursor != undefined) {
            end_cursor_str = '&after=' + end_cursor;
        }
        
        options.url = 'https://www.instagram.com/graphql/query/?query_id=17880160963012870&id=' 
                        + user_id + '&first=' + first + end_cursor_str;
        
        rp(options)
            .then(function (autoParsedBody) {
                if (autoParsedBody.status === "ok") {
                    if (args.debug) console.log(autoParsedBody.data);
                    var posts = autoParsedBody.data.user.edge_owner_to_timeline_media;
                    
                    // POSTS processing
                    if (posts.edges.length > 0) {
                        //console.log(posts.edges);
                        pop_posts = pop_posts.concat
                        (posts.edges.map(function(e) {
                            var d = new Date();
                            var now_seconds = d.getTime() / 1000;

                            var seconds_since_post = now_seconds - e.node.taken_at_timestamp;
                            //console.log("seconds_since_post: " + seconds_since_post);
                            
                            var ageing = 10; // valuses (1-10]; big value means no ageing
                            var days_since_post = Math.floor(seconds_since_post/(24*60*60));
                            var df = (Math.log(ageing+days_since_post) / (Math.log(ageing)));
                            var likes_per_day = (e.node.edge_liked_by.count / df);
                            // console.log("likes: " + e.node.edge_liked_by.count);
                            //console.log("df: " + df);
                            //console.log("likes_per_day: " + likes_per_day);
                            //return (likes_per_day > 10 * 1000);
                            var obj = {};
                            obj.url = e.node.display_url;
                            obj.likes_per_day = likes_per_day;
                            obj.days_since_post = days_since_post;
                            obj.total_likes = e.node.edge_liked_by.count;
                            return obj;
                        }
                        ));
                        
                        pop_posts.sort(function (b,a) {
                          if (a.likes_per_day < b.likes_per_day)
                            return -1;
                          if (a.likes_per_day > b.likes_per_day)
                            return 1;
                          return 0;
                        });
                        
                        //console.log(pop_posts);
                        
                        pop_posts.forEach(function (obj) {
                            console.log(obj.url);
                        });
                    }
                    
                    if (posts.page_info.has_next_page) {
                        GetPostsFromUser(user_id, first, posts.page_info.end_cursor);
                    }
                } else {
                    console.log( "ERROR: Posts AJAX call not returned good..." );
                }
            })
            .catch(function (err) {
                console.log( "ERROR: " + err );
            }); 
    }
}


ScrapeInstagramPage ({username : "dress_blouse_designer", debug : false});

Try it here

Example: For given a URL 'https://www.instagram.com/dress_blouse_designer/'; one may call function

ScrapeInstagramPage ({username : "dress_blouse_designer", debug : false});

Solution 19 - Instagram Api

This works using a simple ajax call and iterating image paths.

        var name = "nasa";
        $.get("https://www.instagram.com/" + name + "/?__a=1", function (data, status) {
            console.log('IG_NODES', data.user.media.nodes);
            $.each(data.user.media.nodes, function (n, item) {
                console.log('ITEMS', item.display_src);
                $('body').append(
                    "<div class='col-md-4'><img class='img-fluid d-block' src='" + item.display_src + "'></div>"
                );
            });
        })

Solution 20 - Instagram Api

Here is a php script that downloads the images and creates an html file with links on the images. Credit 350D for php version, this is just elaborated..I would suggest putting this is a cron job and firing however often you need. Verified working as of May 2019.

<?
$user = 'smena8m';
$igdata = file_get_contents('https://instagram.com/'.$user.'/');
preg_match('/_sharedData = ({.*);<\/script>/',$igdata,$matches);
$profile_data = json_decode($matches[1])->entry_data->ProfilePage[0]->graphql->user;
$html = '<div class="instagramBox" style="display:inline-grid;grid-template-columns:auto auto auto;">';
$i = 0;
$max = 9;
while($i<$max){
	$imglink = $profile_data->edge_owner_to_timeline_media->edges[$i]->node->shortcode;
	$img = $profile_data->edge_owner_to_timeline_media->edges[$i]->node->thumbnail_resources[0]->src;
	file_put_contents('ig'.$i.'.jpg',file_get_contents($img));
	$html .= '<a href="https://www.instagram.com/p/'.$imglink.'/" target="_blank"><img src="ig'.$i.'.jpg" /></a>';
	$i++;
}
$html .= '</div>';
$instagram = fopen('instagram.html','w');
fwrite($instagram,$html);
fclose($instagram);
?>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPeejaView Question on Stackoverflow
Solution 1 - Instagram Api350DView Answer on Stackoverflow
Solution 2 - Instagram ApiErsan J SanoView Answer on Stackoverflow
Solution 3 - Instagram ApiFootnikoView Answer on Stackoverflow
Solution 4 - Instagram ApiMichaelView Answer on Stackoverflow
Solution 5 - Instagram ApiwhizzzkidView Answer on Stackoverflow
Solution 6 - Instagram ApiBill RollinsView Answer on Stackoverflow
Solution 7 - Instagram ApiBenjamin TalismanView Answer on Stackoverflow
Solution 8 - Instagram ApispiceView Answer on Stackoverflow
Solution 9 - Instagram ApiCraig HeneveldView Answer on Stackoverflow
Solution 10 - Instagram ApiVulovic VukasinView Answer on Stackoverflow
Solution 11 - Instagram ApiLeoView Answer on Stackoverflow
Solution 12 - Instagram Apikara4kView Answer on Stackoverflow
Solution 13 - Instagram ApiDorian DamonView Answer on Stackoverflow
Solution 14 - Instagram ApiRahul GusainView Answer on Stackoverflow
Solution 15 - Instagram ApialtinturkView Answer on Stackoverflow
Solution 16 - Instagram ApiRuan BarrosoView Answer on Stackoverflow
Solution 17 - Instagram ApiKarra MaxView Answer on Stackoverflow
Solution 18 - Instagram ApiVishnu KanwarView Answer on Stackoverflow
Solution 19 - Instagram ApiEvin WeissenbergView Answer on Stackoverflow
Solution 20 - Instagram ApidroohView Answer on Stackoverflow