Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all images of hashtag in Instagram without API?

Tags:

php

instagram

This is the code that I used to get images of hashtag without API. I do not want to use any credentials. It does not require me to add either client_id or access token. But I only get 15 images. How can I get all the images?

 <div>

    <form action='#' method='post'>
    <input type='input' name='txttag' />
    <input type='submit' value='Get Image' />
    </form>

    </div>


    <?php 
    function scrape_insta_hash($tag) {
        $insta_source = file_get_contents('https://www.instagram.com/explore/tags/'.$tag.'/'); // instagrame tag url
        $shards = explode('window._sharedData = ', $insta_source);
        $insta_json = explode(';</script>', $shards[1]); 
        $insta_array = json_decode($insta_json[0], TRUE);
        return $insta_array; // this return a lot things print it and see what else you need
    }

    if(isset($_POST['txttag']))
    {
        $tag =$_POST['txttag']; // tag for which ou want images 
        $results_array = scrape_insta_hash($tag);
        $limit = 15; // provide the limit thats important because one page only give some images then load more have to be clicked
        $image_array= array(); // array to store images.
            for ($i=0; $i < $limit; $i++) { 
                $latest_array = $results_array['entry_data']['TagPage'][0]['tag']['media']['nodes'][$i];
                $image_data  = '<img src="'.$latest_array['thumbnail_src'].'">'; // thumbnail and same sizes 
                //$image_data  = '<img src="'.$latest_array['display_src'].'">'; actual image and different sizes 
                array_push($image_array, $image_data);
            }
            foreach ($image_array as $image) {
                echo $image;// this will echo the images wrap it in div or ul li what ever html structure 
            }
            //https://www.instagram.com/explore/tags/your-tag-name/
    }
    ?>



    <style>
    img {
      height: 200px;
      margin: 10px;
    }
    </style>
like image 821
Jigs Parmar Avatar asked Nov 11 '16 06:11

Jigs Parmar


People also ask

How do I see all hashtags on Instagram?

You can see all the hashtags you follow by going to your Instagram profile, clicking following and looking under the Hashtags tab.

Can you scrape Instagram hashtags?

Scraping Instagram hashtags is the fastest, most efficient way to get datasets that you can use anywhere. If you want more ideas, check out our industries pages for ways web scraping is already being used in a wide range of companies.

Why can't I see all hashtags on Instagram?

Hashtags on Instagram may not be searchable if they're consistently associated with content that doesn't follow our Terms of Use or Community Guidelines. We review these hashtags periodically and may make the hashtag searchable again if the content associated with particular hashtags changes over time.

How do I see a hidden hashtag?

Enter a hashtag in the search bar, tap 'Tags', and click the 'Search' button. Click on the hashtag you are looking up, then tap on 'Recent'. If you see a message that posts are hidden instead of seeing recent posts, you've located a banned hashtag.


1 Answers

Easily way is request with ?__a=1 like https://www.instagram.com/explore/tags/girls/?__a=1 and receive JSON without parsing HTML and window._sharedData =

In json you can see page_info scope with end_cursor:

"page_info": {
    "has_previous_page": false,
    "start_cursor": "1381007800712523480",
    "end_cursor": "J0HWCVx1AAAAF0HWCVxxQAAAFiYA",
    "has_next_page": true
},

use end_cursor to request next portion of images:

https://www.instagram.com/explore/tags/girls/?__a=1&max_id=J0HWCVx1AAAAF0HWCVxxQAAAFiYA

UPD:

<?php

$baseUrl = 'https://www.instagram.com/explore/tags/girls/?__a=1';
$url = $baseUrl;

while(1) {
    $json = json_decode(file_get_contents($url));
    print_r($json->tag->media->nodes);
    if(!$json->tag->media->page_info->has_next_page) break;
    $url = $baseUrl.'&max_id='.$json->tag->media->page_info->end_cursor;
}
like image 55
ilyapt Avatar answered Oct 11 '22 19:10

ilyapt