xpath - 如何在 DevTools 控制台中使用 xpath 计算搜索结果中所有 YouTube 直播的总观看次数?
问题描述
我正在尝试计算观看单个活动(迈克尔科恩国会听证会)的多个 YouTube 直播的观众总数,该活动由数十个帐户(有数十万人观看)同时直播。如何使用 xpath 计算 Chrome DevTools 控制台中事件的所有流的总观众数到搜索结果页面//*[@id="metadata-line"]/span
上每个视频的视图计数元素。(我搜索了按直播过滤的“Michael Cohen”,按观看次数排序)
解决方案
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.
推荐阅读
- git - 如何在本地将工作分支 A 合并到新分支 B..?
- python - 将时间序列分组为不均匀的频率
- c - 消费者在数组已满之前不会启动
- python - 如何使用具有索引值的 numpy 数组将另一个 numpy 数组的元素设置为 0?
- c# - .NET Core Jwt 令牌在尝试访问授权区域时总是过期
- c# - 为什么 C# 导入仅在命名空间内有效
- python-3.8 - 如何在 Python 3.8 中为变量添加一个?
- google-cloud-platform - 将流数据从 API 提取到 Google Cloud 中的 Bigquery
- objective-c - 1:many:many 模型中的 NSArrayController 绑定细节
- c# - 当我尝试调用实例对象时出现空引用异常