-
-
Save hardiksondagar/732c5efa1c4327f12d8a343c960853d6 to your computer and use it in GitHub Desktop.
Instagram follower web scrapper.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* 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 <[email protected]> | |
* | |
*/ | |
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; | |
var match_url_string = 'query'; | |
function onReadyStateChange() { | |
if (self.readyState == 4 && url.indexOf(match_url_string) > -1) { | |
var response = JSON.parse(self.response); | |
followers = followers.concat(response.data.user.edge_followed_by.edges); | |
} | |
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,Full Name,Pic,Id\n"; | |
csvContent += header; | |
followers.forEach(function(follower, index) { | |
var data = []; | |
data.push(follower.node.username); | |
data.push(follower.node.full_name); | |
data.push(follower.node.profile_pic_url); | |
data.push(follower.node.id); | |
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 | |
var evt = new MouseEvent("click"); | |
var canceled = !link.dispatchEvent(evt); | |
if (canceled) { | |
// A handler called preventDefault | |
console.log("canceled"); | |
} else { | |
// None of the handlers called preventDefault | |
console.log("not canceled"); | |
} | |
// This will download the data file named "my_data.csv". | |
} |
Is this method still works in 2018
Hey @hardiksondagar, I tested the script, but when I pasted and executed it on chrome, console says "udefined", before calling the "downloadAsCsv() " function. How can I solve this? Thanks in advance.
have the same issue
@carlosMetrics @aliuzair @unnir, I've updated the script, it's working now.
Works with Chrome, but doesn't work with Firefox. It only manages to get the first 17 followers in Firefox. I'll be working on tweaking this to read my followings and then compare the two in excel hopefully. Thanks @hardiksondagar
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is this still working? i have tried it and i am getting this error "Uncaught TypeError: Cannot read property 'edges' of undefined
at XMLHttpRequest.onReadyStateChange (:27:81)". this would be reaaaaly helpful if it worked, Thanks!:)