[英]How to get public user all posts from instagram, without instagram API
我试图从 Instagram 上的公共用户帐户获取帖子,我尝试了几乎所有可能的方法,但它们都返回 403 错误(访问被拒绝)。
$.ajax({
url: URL,
type: "GET",
success: function(data) {
console.log('Success!' ,data);
},
error: function (response) {
console.log('ERROR!', response);
}
});
以上是我试图获取数据的链接。 正如我所读到的 Instagram 正在更改他们的协议,是否还有其他方法可以在不使用 Instagram API 和后端代码的情况下从任何公共用户获取帖子列表?
谢谢你
您的第二个解决方案应该有效。 尝试访问这个网址:
https://www.instagram.com/{username}/?__a=1
它包含用户页面上可用的所有信息,包括 12 篇最新帖子,大小不一。 作为旁注,它不包含关注者或关注列表
我制作了一个使用这种方法的jquery插件:
https://github.com/kasperlegarth/instastory.js
此解决方案适用于 PHP,它可以从一个帐户中获取 12 个最近的帖子。
<?php
$username = "najemi.cz";
$html = file_get_contents("https://www.instagram.com/$username");
$html = strstr($html,'window._sharedData = ');
$html = explode("</script>", $html);
$html = $html[0];
$html = str_replace("window._sharedData = ","",$html);
$html = strstr($html,'"edge_owner_to_timeline_media');
$html = explode(',"edge_saved_media"',$html);
$html = '{'.$html[0].'}';
$html = json_decode($html,true);
for ($i=0; $i < 100; $i++) {
$pos[$i] = $html['edge_owner_to_timeline_media']['edges'][$i]['node']
['display_url'];
echo '<img src="'.$pos[$i].'" ><br>';
}
?>
您收到 403,因为您没有有效的会话。 Web 应用程序会话存储为 Cookie(浏览 instagram.com 时有一个 sessionid cookie)。
您需要在请求的标头中包含会话 cookie 。
获取有效请求的所有标头的一种简单方法是转到开发人员工具的网络部分并复制请求标头或其他格式。
另一种方法是使用Greasemonkey或Tampermonkey脚本。 将直接从浏览器执行并使用当前会话。
您可以在某处添加它,它将从用户那里提取 12 个最近的帖子,并返回数组中照片的链接。 这是一个实现
function getPosts($profile) {
$base = "https://instagram.com/";
$end = "/?__a=1";
$ls = array();
$content = file_get_contents($base.$profile.$end);
if (strpos($content, "is_private\":false") !== false) {
return array(true, array());
}
$split = "config_height\":320},{\"src\":\"";
while (strpos($content, $split) !== false) {
$part = @explode($split, $content, 2)[1];
$p = @explode("\"", $part, 2)[0];
$content = str_replace($split.$p, "", $content);
array_push($ls, $p);
}
return array(false, $ls);
}
$x = getPosts("najemi.cz");
$isPrivate = $x[0];
$posts = $x[1]
if ($isPrivate) {
echo "Sorry, this account is private";
}else{
foreach($posts as $post) {
echo "<img src=\"$post\">";
}
}
这将在 HTML 中用于显示帐户的 12 个最新帖子。 您可以通过添加和删除显示的内容来根据您的需要定制它,但除此之外,可以使用任何存在的用户名调用该函数。
Javascript 如下
function display(array1) {
array1.forEach(element => console.log(element));
}
var username = "najemi.cz";
$.ajax({
url: "./getPosts.php?p=" + username,
type: "GET",
success: function(data) {
display(data.split("\n"));
},
error: function (response) {
console.log('ERROR!', response);
}
});
名为“getPosts.php”的 php 文件将包含:
<?php
function getPosts($profile) {
$base = "https://instagram.com/";
$end = "/?__a=1";
$ls = array();
$content = file_get_contents($base.$profile.$end);
if (strpos($content, "is_private\":false") !== false) {
return array(true, array());
}
$split = "config_height\":320},{\"src\":\"";
while (strpos($content, $split) !== false) {
$part = @explode($split, $content, 2)[1];
$p = @explode("\"", $part, 2)[0];
$content = str_replace($split.$p, "", $content);
array_push($ls, $p);
}
return array(false, $ls);
}
if(isset($_GET['p'])){$p = $_GET['p'];}
$x = getPosts($p);
$isPrivate = $x[0];
$posts = $x[1]
if ($isPrivate) {
echo "Sorry, this account is private";
}else{
foreach($posts as $post) {
echo "$post\n";
}
}
?>
只需使用一些 CSS 选择器,您就可以做到这一点。 将此代码粘贴到您的 chrome 控制台中,您将获得某个用户的所有帖子(无需登录 instagram):
var allLinks = document.getElementsByTagName("a")
var allPosts = []
for (var i = 0; i<allLinks.length; i++){
var isPost = allLinks[i].parentNode.className.indexOf("v1Nh3")>-1;
if (isPost)
allPosts.push(allLinks[i].href)
}
console.log(allPosts)
最后,如果您想改进这一点,请添加一些分页并重复相同的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.