如何从 Instagram 检索视频观看次数 API

How to retrieve video view count from Instagram API

Instagram 最近开始显示视频的观看次数。有没有办法从 API 中提取这些数据?

我通读了 documentation 但我找不到任何关于 "Views" 的信息,只有 "Likes".

尚未通过 public API 提供。

我发现的唯一方法是使用像 Seleniuim 这样的浏览器自动化系统地抓取帖子的永久链接(使用一些逻辑处理格式,例如 5.6k 视图与 1,046 视图)并挑选出适当的元素。由于未检测到 javascript,简单的 GET 请求不会产生所需的 DOM。

在python中:

from bs4 import BeautifulSoup
from selenium import webdriver

def insertViews(posts):
    driver = webdriver.PhantomJS('<path-to-phantomjs-driver-ignoring-escapes>')
    views_span_dom_path = '._9jphp > span'

    for post in posts:
        post_type = post.get('Type')
        link = post.get('Link')
        views = post.get('Views')

        if post_type == 'video':
            driver.get(link)
            html = driver.page_source

            soup = BeautifulSoup(html, "lxml")
            views_string_results = soup.select(views_span_dom_path)
            if len(views_string_results) > 0:
                views_string = views_string_results[0].get_text()
            if 'k' in views_string:
                views = float(views_string.replace('k', '')) * 1000
            elif ',' in views_string:
                views = float(views_string.replace(',', ''))
            elif 'k' not in views_string and ',' not in views_string:
                views = float(views_string)
        else:
            views = None

        post['Views'] = views
    driver.quit()
    return posts

可以下载 PhantomJS 驱动 here

是的,你可以得到它,如果你有你的 facebook 和 instagram 帐户关联,并且你的 instagram 帐户有企业资料,请发出以下 GET 请求:

https://graph.facebook.com/v3.0/instagram_video_id/insights/video_views

您将收到以下格式的回复:

{
  "data": [
    {
      "name": "video_views",
      "period": "lifetime",
      "values": [
        {
          "value": 123
        }
      ],
      "title": "Video Views",
      "description": "Total number of times the video has been seen",
      "id": "instagram_video_id/insights/video_views/lifetime"
    }
  ]
}