[英]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.