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

前端 未结 4 1966
执笔经年
执笔经年 2020-12-08 03:40

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 acces

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-08 03:56

    The Answer from Legionar was great but it is not working anymore. I had to update the code in my working Environment here is how it works for me:

    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(';', $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
    }
    
    $tag = "my_hashtag";
    $results_array = scrape_insta_hash($tag);
    
    $limit = 18; // provide the limit thats important because one page only give some images then load more have to be clicked
    
    for ($i=$limit; $i >= 0; $i--) {
      if(array_key_exists($i,$results_array['entry_data']['TagPage'][0]["graphql"]["hashtag"]["edge_hashtag_to_media"]["edges"])){
        $latest_array = $results_array['entry_data']['TagPage'][0]["graphql"]["hashtag"]["edge_hashtag_to_media"]["edges"][$i]["node"];
    
          $newPosting = [
            "image"=>$latest_array['display_url'],
            "thumbnail"=>$latest_array['thumbnail_src'],
            "instagram_id"=>$latest_array['id'],
            "caption"=>$latest_array['caption']['edge_media_to_caption']['edges'][0]["node"]["text"],
            "link"=>"https://www.instagram.com/p/".$latest_array['shortcode'],
            "date"=>$latest_array['taken_at_timestamp']
          ];
    
          echo "
    "; 
          print_r($newPosting); 
          echo "/
    "; 
    
      }
    }
    

    You may need to change the "newPosting" array depending on what you need but at least for now you can get the instagram data with this method. Also there is more data inside $latest_array . Different image-sizes, comments and likes for example.

提交回复
热议问题