获取 Instagram 关注者数量

Grab Instagram Follower count

所以我有一个问题,即获取所述用户的 Instagram 关注者数量的方法是什么?

我查看了官方 instagram API 的两个可能选项,但我找不到关于如何操作的特定方法,但它们确实有一些用户端点,但找不到找到很多关于它的细节或者我正在考虑使用非官方的 instagram API https://github.com/mgp25/Instagram-API

有什么建议吗?

您可以请求 https://www.instagram.com/<username>/?__a=1 并接收 JSON 以及帐户信息和关注者数量。不需要授权。

接受的答案 (https://www.instagram.com/<username>/?__a=1) 中的 link 似乎不再有效,但我们仍然可以通过解析正常配置文件 [=30] 中的 html 来获得关注者数量=] https://www.instagram.com/<username>

如果您执行 GET 请求,您将获得普通的 HTML 并且您可以搜索看起来像 <link rel="canonical" href="https://www.instagram.com/<username>/" /><meta content="359 Followers, 903 Following, 32 Posts - See Instagram photos and videos from <username>)" name="description" />[=15= 的 html 标签]

您可以在浏览器中转到 Instagram 个人资料进行试用,然后右键单击并查看页面源代码。然后只需解析文本即可获取所需信息。

下面是获取 javascript 中关注者数量的示例:

var url = "https://www.instagram.com/username";
request.get(url, function(err, response, body){
    if(response.body.indexOf(("meta property=\"og:description\" content=\"")) != -1){
        console.log("followers:", response.body.split("meta property=\"og:description\" content=\"")[1].split("Followers")[0])
    }
 });

这可能不是一种可靠的、面向未来的方法,但它目前似乎确实有效。

当您向任何 Instagram 个人资料发出 GET 请求并尝试获取 HTML 时,您会在代码中看到类似 window._sharedData = "..." 的内容。

然后您基本上获取了该变量的内容并将其转换为一个对象以访问其中的任何数据。然后你控制台日志json.entry_data.ProfilePage[0].graphql.user,你会得到关于用户的所有信息。

这是一个 Axios 示例。

const axios = require('axios')

const run = async () => {
  try {
    const post = await axios({
      method: 'get',
      url: 'https://www.instagram.com/ozgrozer/'
    })

    const pattern = /<script type="text\/javascript">window._sharedData = ([\s\S]*?);<\/script>/gi
    const matches = pattern.exec(post.data)
    const scriptContent = matches[1]
    const json = JSON.parse(scriptContent)

    if (json.entry_data.LoginAndSignupPage) throw new Error('Login required')

    console.log(json.entry_data.ProfilePage[0].graphql.user)
    /*
      {
        "id": "7851687561",
        "full_name": "Ozgur Ozer",
        "edge_follow": { "count": 87 },
        "biography": "American oxygen ",
        "edge_followed_by": { "count": 102 },
        "external_url": "http://about.me/ozgrozer",
      }
    */
  } catch (err) {
    console.log(err)
  }
}

run()

更新:在上面的几个 运行 代码之后,我意识到 Instagram 开始 return LoginAndSignupPage: []json.entry_data 中的某些东西并且基本上阻止了请求。不知道现在该做什么。也许使用代理或 Tor 连接?