如何使用querySelectorAll获取YouTube频道名称?

问题描述

OUTPUT : Console

URL: https://www.youtube.com/feed/trending?gl=IN

document.querySelectorAll('a[class="yt-simple-endpoint style-scope yt-formatted-string"]')[0].innerText;

document.querySelectorAll('a[class="yt-simple-endpoint style-scope yt-formatted-string"]')[1].innerText;

问题::为什么控制台上的OUTPUT中有重复项(单击上图)?有人可以解释这里发生了什么吗?如何获取 唯一频道名称

解决方法

尝试一下:

document.querySelectorAll('#meta a.yt-simple-endpoint.style-scope.yt-formatted-string')[1].innerText;

中还有另一个隐藏的元素,其ID为'channel-info',具有相同的类集。您可以按F12,然后按CTRL-F来搜索该组类以查看重复的类。