I thought I’d take a stab at using Google Script to capture Instagram data after being inspired by all the great Martin Hawksey has done with the TAGS Explorer. I’m doing something very similar and it turned out to be fairly straightforward to take the PHP I’d written previously and turn it into something that’d function in Google Sheets. I found this example highly useful in creating my own script.1

The following script can also be set with a time based trigger to fire every X minutes/hours/days which is perfect for this particular project.

Google Script Trigger

You can see it pulling 20 pictures worth of data every hour here if you’d like.

function addTitles() {
  var sheet = SpreadsheetApp.getActiveSheet();
  // sets up the header row for the sheet
  sheet.appendRow(['User Name', 'Likes Count', 'Comment Count', 'URL', 'Caption', 'Filter', 'Created Time']);
}

function getInstagramData(searchTerm) {
	
	var search = 'vape'; // search term
	var url = 'https://api.instagram.com/v1/tags/' + search +'/media/recent?client_id=XXXXXXXXXXXXXXXXXX'; // replace this with your API client ID
	
	var  result = UrlFetchApp.fetch(url);
	
	if (result.getResponseCode() == 200) {
		var json = JSON.parse(result.getContentText());
		
		var grams = json.data;
        Logger.log(result);

		// Instagram gives you 20 on the first request
		for (var i = 0; i<=20; i++) {
			logGram_(grams[i]);
		}
	}
}


function logGram_(gram) {
	var log = [];
// pushes the various elements into the spreadsheet	
    log.push(gram.user.username);
    log.push(gram.likes.count);
    log.push(gram.comments.count);
    log.push(gram.link);
    log.push(gram.caption.text);
    log.push(gram.filter);
    log.push(gram.created_time);
  
	SpreadsheetApp.getActiveSheet().appendRow(log)
}

1 I basically copied portions of it.

9 thoughts on “Scraping Instagram with Google Script

  1. This looks really useful — but did you need to create a spreadsheet for it to push the data to? I’m assuming so, but I don’t see any info about configuring it in your instructions.

    Thanks!

  2. Thanks a lot for building and sharing this! 🙂

    But what about getting more than 20 pics? Is there a way to do that?
    Also, I get this error when I run the function (results still do appear though!) :

    ReferenceError: “logGram_” is not defined. (line 22, file “”)

    1. Here’s how I did that with PHP. This version fetches 100 entries. If you don’t have access to a server, you can run it on something virtual like Vagrant.

      I meant to cycle back around and add that to the Google version but haven’t gotten to it yet.

      I don’t really understand those errors either. Everything seems to work just fine and the elements that referenced in my errors are there . . . For now, I just delete the emails. Sad but true.

      It’s also worth noting that I only vaguely know what I’m doing. 🙂

  3. I get an error any time I try to run this. Is this script still valid with the new API changes to IG? Request failed for https://api.instagram.com/v1/tags/feedfeed/media/recent?client_id=(ACTUAL CLIENT ID REMOVED HERE) returned code 400. Truncated server response: {“meta”: {“error_type”: “OAuthAccessTokenException”, “code”: 400, “error_message”: “The access_token provided is invalid.”}} (use muteHttpExceptions option to examine full response) (line 16, file “Code”)
    OK

    1. That URL works with my client ID. It may be that they aren’t allowing it on newer API registrations.

      Were I doing this again, and I might in the near future, I’d do the full oauth and probably use this.

Leave a Reply