Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Quick-and-dirty Instagram web scrape, just in case you don't think you should have to make your users log in to deliver them public photos.
<?php
//returns a big old hunk of JSON from a non-private IG account page.
function scrape_insta($username) {
$insta_source = file_get_contents('http://instagram.com/'.$username);
$shards = explode('window._sharedData = ', $insta_source);
$insta_json = explode(';</script>', $shards[1]);
$insta_array = json_decode($insta_json[0], TRUE);
return $insta_array;
}
//Supply a username
$my_account = 'cosmocatalano';
//Do the deed
$results_array = scrape_insta($my_account);
//An example of where to go from there
$latest_array = $results_array['entry_data']['ProfilePage'][0]['user']['media']['nodes'][0];
echo 'Latest Photo:<br/>';
echo '<a href="http://instagram.com/p/'.$latest_array['code'].'"><img src="'.$latest_array['display_src'].'"></a></br>';
echo 'Likes: '.$latest_array['likes']['count'].' - Comments: '.$latest_array['comments']['count'].'<br/>';
/* BAH! An Instagram site redesign in June 2015 broke quick retrieval of captions, locations and some other stuff.
echo 'Taken at '.$latest_array['location']['name'].'<br/>';
//Heck, lets compare it to a useful API, just for kicks.
echo '<img src="http://maps.googleapis.com/maps/api/staticmap?markers=color:red%7Clabel:X%7C'.$latest_array['location']['latitude'].','.$latest_array['location']['longitude'].'&zoom=13&size=300x150&sensor=false">';
?>
*/

Vheissu commented Feb 17, 2014

Amazingly simple and effective. Thank you for sharing this.

Hi,
How can one go about running this script?
Thanks

made a similar tool over here, to get a user's entire history: https://github.com/slang800/instagram-screen-scrape

Here's a quick class to only get the image from an Instagram image url:

<?php 

class InstagramScraper
{
    protected $content;
    function __construct($url)
    {
        $this->content = @file_get_contents($url);
    }

    public function image(){
        preg_match('#<meta +property=\\"og:image\\" +content=\\"(http.+?\.jpg)\\"#', $this->content, $result);
        return $result[1];
    }
}
?>

Still working given the recent changes to Instagram?

Nimoi commented Dec 8, 2015

Thanks! I needed to scrape some individual posts. This still works.

lmj0011 commented Dec 16, 2015

if you want to get back a certain number of images, just increment the $results_array like so:

 for($cnt=0; $cnt < 20; $cnt++)
{
 $latest_array = $results_array['entry_data']['ProfilePage'][0]['user']['media']['nodes'][$cnt];

 echo 'Latest Photo:<br/>';
 echo '<a href="http://instagram.com/p/'.$latest_array['code'].'"><img src="'.$latest_array['display_src'].'"></a></br>';
 echo 'Likes: '.$latest_array['likes']['count'].' - Comments: '.$latest_array['comments']['count'].'<br/>';
}

Thanks for sharing this gist

Thank you so much for this, love the quick and dirty-ness of it! I have a couple of questions if you can help.

  • How can I set the number of posts to display? I'd like to show 2 or 4 of the latest
  • Is it possible to get the images by location ID using a slightly modified method? That's what I was initially trying to do, if it's not possible the userID works ok as well.

Thanks again.

ravij28 commented Jun 2, 2016

It's showing below error:

Warning: file_get_contents(http://instagram.com/ascia_akf): failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\wamp\www\Trial\curl.php on line 4

Fatal error: Maximum execution time of 30 seconds exceeded in C:\wamp\www\Trial\curl.php on line 4

pistell commented Jun 2, 2016

@cosmocatalano I seriously owe you a beer or twelve for this. The Instagram API migration ruined my app and this saved me.

Awesome script, anyway to get more than 15 photos though? Or get the ajax from the load more button?

Script works fine but can you please guide me to get more then 15 posts ?

I've made web scrapper to get user X's followers using javascript that stores followers and exports in csv files. All you need to do is to keep scrolling.

/**
 * 
 * Instagram Follower Web Scrapper 
 * 
 * Steps to use.
 * 1. Open instagram user's profile in browser https://www.instagram.com/tvfpitchers/
 * 2. Open console ( press F12 in chrome ) and paste all the code below
 * 3. Click on followers button and load all the followers
 * 4. Call function downloadAsCsv() by writing "downloadAsCsv()" in console to download csv file containing user's all the followers .
 * 
 * @author : Hardik Sondagar <hardikmsondagar@gmail.com>
 * 
 */

var followers = [];

(function(XHR) {
    "use strict";

    var stats = [];
    var timeoutId = null;

    var open = XHR.prototype.open;
    var send = XHR.prototype.send;

    XHR.prototype.open = function(method, url, async, user, pass) {
        this._url = url;
        open.call(this, method, url, async, user, pass);
    };

    XHR.prototype.send = function(data) {
        var self = this;
        var start;
        var oldOnReadyStateChange;
        var url = this._url;

        function onReadyStateChange() {
            if(self.readyState == 4 && url == 'https://www.instagram.com/query/') {

              var response = JSON.parse(self.response);
              followers = followers.concat(response.followed_by.nodes);

            }

            if(oldOnReadyStateChange) {
                oldOnReadyStateChange();
            }
        }

        if(!this.noIntercept) {
            start = new Date();

            if(this.addEventListener) {
                this.addEventListener("readystatechange", onReadyStateChange, false);
            } else {
                oldOnReadyStateChange = this.onreadystatechange; 
                this.onreadystatechange = onReadyStateChange;
            }
        }

        send.call(this, data);
    }
})(XMLHttpRequest);

function downloadAsCsv() {



    var csvContent = "data:text/csv;charset=utf-8,";

    var header = "Username,Requested_by_viewer,Followed_by_viewer,Profile_pic_url,Full Name,is_verified,Id\n";
    csvContent += header;

    followers.forEach(function(infoArray, index){

        var data = $.map(infoArray, function(value) {
        return [value];
        });

        dataString = data.join(",");
        csvContent += index < followers.length ? dataString+ "\n" : dataString;

    });

    var encodedUri = encodeURI(csvContent);
    var link = document.createElement("a");
    link.setAttribute("href", encodedUri);

    var pathArray = window.location.pathname.split('/');

    var milliseconds = (new Date).getTime();
    var filename = 'followers.'+ milliseconds +'.csv';

    if(pathArray && pathArray.length > 1) {
        filename = pathArray[1] + '.' + milliseconds +'.csv';
    }
    link.setAttribute("download",filename);
    document.body.appendChild(link); // Required for FF

    link.click(); // This will download the data file named "my_data.csv".

}

sripadks commented Jul 1, 2016

@hardiksondagar when I try to use your script, chrome just says "undefined" when I both enter the code into the console as well as run the function. The downloaded CSV is empty.

@hardiksondagar same result.

pipep commented Sep 20, 2016

Sorry for my ignorance, i'm starting with this.. how could I get more than 1 username to be processed on one page ? I get an error all the time.. I need ig-A + ig-B + ig-C latest pictures on one page.. any ideas ?

Thank you !

Does anyone know if this is still working? My get_file_contents() is returning the Instagram signup page. Have then since done some sort of scrape prevention?

How to scrape register emails with Instagram accounts ?

Thanks, this worked great! I modified it to get the 12 most recent photos:

<?php
//returns a big old hunk of JSON from a non-private IG account page.
function scrape_insta($username) {
	$insta_source = file_get_contents('http://instagram.com/'.$username);
	$shards = explode('window._sharedData = ', $insta_source);
	$insta_json = explode(';</script>', $shards[1]);
	$insta_array = json_decode($insta_json[0], TRUE);
	return $insta_array;
}
//Supply a username
$my_account = 'my_instagram_username';
$results_array = scrape_insta($my_account);

// Create a comma-separated list of the first 12 image source URLs.
$url_list = $results_array['entry_data']['ProfilePage'][0]['user']['media']['nodes'][0]['display_src'];
for ($i=1; $i < 12; $i++) {
	$url_list .= ',' . $results_array['entry_data']['ProfilePage'][0]['user']['media']['nodes'][$i]['display_src'];
}

// Print out the list. Use Ajax.get() or something to call this script, then parse it on client: list.split(',').
echo $url_list;
?>

BHFR commented Mar 11, 2017

Thanks for sharing bro !

just add "?__a=1" at the end of url, it's already JSON
ex:
"http://instagram.com/username/?__a=1" for username
"http://instagram.com/tags/hashtag/?__a=1" for hashtag

Spandan-Madan commented Apr 3, 2017 edited

Is this still working? Planning to use it for a research project!

edogaafx commented Apr 10, 2017 edited

Thank You for sharing code! Helped a lot with possibility to avoid API.
@adi64bit thanks, that made things way more easier! Maybe you could share link, where can get more this kind of useful links?

If any of you guys need an instagram nodejs bot, check out my profile repo :)

TIP: If you want square images (1x1 ratio), replace ['display_src'] with ['thumbnail_src'].

Does anyone know what you add to the script to grab the captions from all post in the account. when I added @lmj0011 edits it just gave me the same post over and over again instead of jumping to the next post.

jakim commented Jun 5, 2017

Nice find @adi64bit, thanks!

aehacker commented Jul 6, 2017

**can we get all image from instagram account ? (detect html code and get image urls ? )

Help me to create it!!**

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment