如何在DevTools控制台中使用xpath计算搜索结果中所有YouTube直播视频的总观看次数?

问题描述 投票:0回答:1

我正在计算观看单个活动(Michael Cohen Congressional Hearing)的多个YouTube直播流的观众总数,这些活动由几十个帐户(有数十万人观看)同时进行直播。如何使用xpath //*[@id="metadata-line"]/span将Chrome DevTools控制台中事件的所有流的总观众数量计入search results page上每个视频的视图计数元素。 (我搜索了“Michael Cohen”按实时过滤,按视图排序)enter image description here

xpath youtube console devtools
1个回答
0
投票

您可以使用JS路径:

// 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重新加载页面时,item-section-的值会有所不同。此外,此示例仅检索当前页面中可用的可视化。

您还可以使用YouTube Data API获取这些结果。这是你可以使用的demo

© www.soinside.com 2019 - 2024. All rights reserved.