Search code examples
phpcurlweb-scrapinginstagram

Getting Instagram's followers data using Curl


I'm trying to fetch the number of followers of an instagram account through web scraping and curl. Using their API may be easier but i want to know why this won't work, because in many case i got the data through HTML.

static $url='https://www.instagram.com/cats_of_instagram/';

function getUrlContent($url){
    try {
    $curl_connection = curl_init($url);
    curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);

    //Data are stored in $data
    $data = (curl_exec($curl_connection));
        $position = strpos($data,"<span data-reactid=\".0.1.0.0:0.1.3.1.0.2\"> followers</span>");
        print_r($position);


     curl_close($curl_connection);
    } catch(Exception $e) {
    return $e->getMessage();
    }
}

Problem is the function strpos does not return position.

$position = strpos($data,"<span data-reactid=\".0.1.0.0:0.1.3.1.0.2\"> followers</span>");

Solution

  • You can't do that.

    The element you're looking for is rendered by javascript, after the page has loaded.

    curl doesn't wait for scripts to run (nor does it run any). It just returns the html.

    You can easily verify this by printing $data. Or by looking at the page's source.

    To "see" the element you're looking for, you need to use the DOM inspector.