提问者:小点点

如何从Instagram获取公共用户所有帖子,没有instagramAPI


我试图从Instagram上的公共用户帐户中获取帖子,我几乎尝试了所有可能的方法,但它们都返回了403个错误(访问被拒绝)。

>

  • https://www.instagram.com/graphql/query/?query_hash=ded47faa9a1aaded10161a2ff32abb6b

    https://www.instagram.com/{user-name}/?__a=1

    https://www.instagram.com/{user-name}/media

    $.ajax({
        url: URL,
        type: "GET",
        success: function(data) {
            console.log('Success!' ,data);
        },
        error: function (response) {
            console.log('ERROR!', response);
        }
    });
    

    以上是我试图获取数据的链接。正如我所读到的,Instagram正在改变他们的协议,还有什么方法可以在不使用InstagramAPI和不使用后端代码的情况下从任何公共用户那里获取帖子列表吗?

    谢谢你


  • 共3个答案

    匿名用户

    您的第二个解决方案应该可以工作。尝试访问此网址:

    https://www.instagram.com/{username}/?__a=1
    

    它包含用户页面上可用的所有信息,包括12个最新帖子,大小不同。作为附带说明,它不包含关注者或关注列表

    我制作了一个使用此方法的jquery插件:

    https://github.com/kasperlegarth/instastory.js
    

    匿名用户

    您可以将其添加到某个地方,它将从用户那里提取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\">";
        }
    }
    

    这将在超文本标记语言中用于显示帐户的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);
        }
    });
    

    名为“getPost. 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)
    

    最后,如果您想改进这一点,请添加一些分页并重复相同的代码。