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