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

时间:2016-02-28 18:29:47

标签: instagram-api

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

我通读documentation,但我找不到任何关于"观看"只有"喜欢"。

3 个答案:

答案 0 :(得分:4)

尚未通过公共API提供。

答案 1 :(得分:3)

我发现的唯一方法是系统地搜集帖子'使用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

答案 2 :(得分:1)

是的,你可以得到它,如果你的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"
    }
  ]
}