首页 > 解决方案 > 如何在 DevTools 控制台中使用 xpath 计算搜索结果中所有 YouTube 直播的总观看次数?

问题描述

我正在尝试计算观看单个活动(迈克尔科恩国会听证会)的多个 YouTube 直播的观众总数,该活动由数十个帐户(有数十万人观看)同时直播。如何使用 xpath 计算 Chrome DevTools 控制台中事件的所有流的总观众数到搜索结果页面//*[@id="metadata-line"]/span上每个视频的视图计数元素。(我搜索了按直播过滤的“Michael Cohen”,按观看次数排序) 在此处输入图像描述

标签: xpathyoutubeconsoledevtools

解决方案


You can use JS path instead:

// Get the total of "li" elements in the current page:
var qtyResultsPerPage = document.querySelectorAll('#item-section-185938 > li').length;

// Variable which will get the sum of all visualizations.
var totalViewsPerPage = 0;

// Loop the "li" elements:
for (var incr = 1; incr < qtyResultsPerPage; incr++) {

  // Sum in the "totalViewsPerPage" variable the retrieved value in the iteration.
  // NOTE: The "dot" is removed (excluding also the words).
  totalViewsPerPage += parseFloat(document.querySelector('#item-section-185938 > li:nth-child(' + incr + ') > div > div > div.yt-lockup-content > div.yt-lockup-meta > ul > li').innerText.split(" ")[0].replace(".", ""));
}

// Show the total:
console.log("Total views: " + totalViewsPerPage);

N.B the value of item-section- vary when the page is reloaded. Also, this sample retrieves only the visualizations available in the current page.

You can also use the YouTube Data API for get these results. This is a demo you can use.


推荐阅读