Sebastien Horin Sebastien Horin - 2 months ago 52
Javascript Question

Scraping data from Instagram

I actually just need the followers count of a public account,
for example https://www.instagram.com/kygomusic/

The new Instagram's API rules are very strict (and discussed):
It's now impossible to access public content for most of common apps. You need a public_content scope that is not granted to normal app (?!)


public_content: This permission (public_content) is only granted to
apps that enable brands, advertisers, broadcasters and publishers to
discover public content. We do not grant access to apps that do not
fall into these categories. Please review our documentation
(https://www.instagram.com/developer/review) for more information.


So I decided to scrape the data from Instagram



An option is to use file_get_contents() (PHP) and it works but it loads all the site from my server and it's pretty heavy. So my first idea was to use YQL. I use it for Twitter and works well, but when I scrape data from Instagram I get nothing:

http://developer.yahoo.com/yql/console/?q=select%20*%20from%20html%20where%20url%3D'https%3A%2F%2Fwww.instagram.com%2Fkygomusic%2F'&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys

Answer

I took a look into the page you submitted, it's not that heavy, considering that you won't load images or process js. While inspecting I found out that they have a json where they store their data.

.... "followed_by": {"count": 924725}

I didn't had time to test this, but it should work, or at least you get the point of using it. CURL may be a better option because it can handle multithreaded requests.

$url = 'https://www.instagram.com/kygomusic/';
$str = file_get_contents($url);
$count = 0;
if(preg_match('#followed_by": {"count": (.*?)}#', $str, $match)) {
     $count = $match[1]; // get the count from Regex pattern
} echo $count;
Comments