Skip to content

Instantly share code, notes, and snippets.

@renatodex
Created July 14, 2018 00:05
Show Gist options
  • Save renatodex/7136bc66a8545516d50472bedea591fb to your computer and use it in GitHub Desktop.
Save renatodex/7136bc66a8545516d50472bedea591fb to your computer and use it in GitHub Desktop.
Instagram Comment Scrapper
// Adicionando jQuery
var jq = document.createElement('script');
jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
// ... give time for script to load, then type (or see below for non wait option)
jQuery.noConflict();
// Função Recursiva que faz o Scrap
function scrap_comments(nodeset = [], query_hash, variables) {
result = []
pageNodeset = []
encoded_variables = encodeURIComponent(JSON.stringify(variables))
url = `https://www.instagram.com/graphql/query/?query_hash=${query_hash}&variables=${encoded_variables}`
console.log(url)
$.get(url).then((r) => {
console.log("URL")
var edge = r.data.shortcode_media.edge_media_to_comment
var edgeNodes = edge.edges
for(var i in edgeNodes) {
pageNodeset.push([
edgeNodes[i].node.owner.username,
edgeNodes[i].node.text
])
}
nodeset.push(pageNodeset)
if (edge.page_info.has_next_page) {
variables.after = edge.page_info.end_cursor
console.log(pageNodeset)
scrap_comments(nodeset, query_hash, variables)
} else {
result = nodeset
}
})
return result
}
// Chamada do Scrapper. Precisa passar o Media Hash, e o Hash de Variaveis que tem a paginação.
scrap_comments([], "f0986789a5c5d17c2400faebf16efd0d", {
"shortcode":"Bk-ReSPBxkk",
"first":43,
"after":"AQBA1mMEeDIdSQxiPbotR-48lCbnbM6e-gAHmXnf5B0oF5gITcQtM0_hjCjyE5ocZ3jXv6lTI9jkgUBpwnAOZuWrxqNmRpjzQK0FI1gylecTjg"
})
// Ah, e precisa estar na página do Instagram também pra evitar problema de CrossDomain.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment