这是我用来在没有API的情况下获取标签图像的代码。我不想使用任何凭证。它不需要我添加client_id
或访问令牌。但我只有15张图片。我怎样才能得到所有的图像?
<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>
简单的方法是请求?__a=1
如https://www.instagram.com/explore/tags/girls/?__a=1
和接收JSON而不解析HTML和window._sharedData =
"page_info": {
"has_previous_page": false,
"start_cursor": "1381007800712523480",
"end_cursor": "J0HWCVx1AAAAF0HWCVxxQAAAFiYA",
"has_next_page": true
},
使用end_cursor请求下一部分图像:
https://www.instagram.com/explore/tags/girls/?__a=1&max_id=J0HWCVx1AAAAF0HWCVxxQAAAFiYA
乌利希期刊指南:
<?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;
}
Legionar的答案很棒,但它不再工作了。我必须在我的工作环境中更新代码,下面是它对我的工作方式:
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
}
$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 "<pre>";
print_r($newPosting);
echo "/<pre>";
}
}
您可能需要根据需要更改"newPosting"数组,但至少现在您可以使用此方法获取instagram数据。此外,在$latest_array中有更多的数据。不同的图片大小,评论和点赞。
这个很适合我。
我只需要缩略图。您可以轻松地将其更改为全尺寸图像。这个例子没有解决分页问题,但是您可以通过@ilyapt answer来解决这个问题。
$tag = 'coronavirus';
$json = json_decode(file_get_contents("https://www.instagram.com/explore/tags/$tag/?__a=1", true));
$i = 0;
foreach($json->graphql->hashtag->edge_hashtag_to_media->edges as $key => $value) {
$img = $value->node->thumbnail_resources[0]->src;
echo "<img src='$img'>";
if (++$i == 9) break; // limit to the 9 newest posts
}
@olaf的回答对我来说很有效!
@Tomas该限制是函数将返回的帖子数,因此它不会返回所有帖子。
还有:这个函数将Instagram帖子按从旧到最新的顺序排列。如果您想要最新的是第一个,并返回到限制数:
改变for ($i=$limit; $i >= 0; $i--)
for ($i=0; $i < $limit; $i++)