这是我用来获取没有API的hashtag图像的代码。我不想使用任何凭证。无需添加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>
答案 0 :(得分:44)
轻松方式是使用?__a=1
https://www.instagram.com/explore/tags/girls/?__a=1
之类的请求,并在不解析HTML和window._sharedData =
在json中,您可以使用 end_cursor 查看 page_info 范围:
"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
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;
}
答案 1 :(得分:1)
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>";
}
}
您可能需要更改&#34; newPosting&#34;数组取决于您的需要,但至少现在您可以使用此方法获取instagram数据。此外,$ latest_array中还有更多数据。例如,不同的图像大小,评论和喜欢。
答案 2 :(得分:1)
这个完美地适合我。
我只需要缩略图。您可以轻松地将其更改为全尺寸图像。 此示例无法解决分页,但是您可以从@ilyapt答案中做到这一点。
$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
}
答案 3 :(得分:0)
@olaf回答对我很有用!
@Tomas限制是函数返回的帖子数,因此它不会返回所有帖子。
此外:此功能将Instagram帖子按从最旧到最新的顺序排列。如果您希望最新的是第一个并且向后移动到限制数字:
更改
for ($i=$limit; $i >= 0; $i--)
到
for ($i=0; $i < $limit; $i++)