我正在嘗試在 youtube 頻道視頻選項卡上記錄所有標題、href 和元資訊。所以我去了 chrome 開發控制臺并輸入了下面的代碼。它當前正確記錄元資訊,但標題和 href 僅記錄先前回圈的重復項。我獲取標題、href 和元資訊的方式是正確的,唯一的問題是我回圈的方式。我在另一個回圈中有一個回圈,因為我想在一行中記錄同一視頻的所有資訊,但是雖然可以使用相同的 id 找到標題和 href,但可以使用不同的 id 找到元資訊。如何修復我的代碼,以便我可以在一行中正確記錄同一視頻的標題、href 和元資訊。提前致謝。
復制: go to youtube => go to a specific youtube channel => go to the video tab of their channel => (Optional) Write code to scroll all the way down, to get more data => Then paste in my code to console and run
$('.ytd-grid-video-renderer').find('#video-title').each(function() {
const v1 = $(this).attr('title')
$('.ytd-grid-video-renderer').find('#metadata-line').each(function() {
const v2 = $(this).text()
console.log(`title: ${v1}, meta: ${v2}}`)
})
})
基于@james 代碼更新:但仍有重復項被推送到陣列中。如何修復代碼以使重復項不會被推送到陣列中?
let myArr = []
$('.ytd-grid-video-renderer').each(function() {
const v1 = $(this).find('#video-title').attr('title');
const v2 = $(this).find('#metadata-line').text();
if (v1 && v2) {
myArr.push(`{title: ${v1}, meta: ${v2}}`)
}
})
console.log(myArr)
uj5u.com熱心網友回復:
我相信每個“ytd-grid-video-renderer”都需要一組輸出,因此這些元素應該有一個回圈。
document.querySelectorAll('.ytd-grid-video-renderer').forEach(function(el) {
const v1 = el.querySelector('#video-title');
const v2 = el.querySelector('#metadata-line');
if (v1 && v2) {
console.log(`title: ${v1.getAttribute("title")}, meta: ${v2.innerText}}`)
}
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/505178.html
上一篇:使用資料目標更改類
