免責宣告:我不懂 JS,這是我的作業任務。
有人要求我使用 Chrome 控制臺從 Google 營銷頁面中提取一些資料。
我需要抓取的 HTML 代碼如下所示:
控制臺中的代碼
我制作了以下代碼,在其中創建了一個字典,然后用來document.Selector()獲取資料,但我只從一行中獲取它。
var IntegrationDetails = [{}]
IntegrationDetails[0]["Property Name"] = document.querySelector("JSPath").innerText
IntegrationDetails[0]["Advertiser"] = document.querySelector("JSPath").innerText
IntegrationDetails[0]["Tracking ID"] = document.querySelector("JSPath").innerText
IntegrationDetails[0]["Account"] = document.querySelector("JSPath").innerText
IntegrationDetails[0]["Organisation name"] = document.querySelector("JSPath").innerText
IntegrationDetails[0]["Integrations with reporting data"] = document.querySelector("JSPath").innerText
IntegrationDetails[0]["Integrations with Cost Data"] = document.querySelector("JSPath").innerText
IntegrationDetails[0]["Integrations with Remarketing Lists"] = document.querySelector("JSPath").innerText
copy(IntegrationDetails)
我不知道如何遍歷 mat-tab 元素中的每一行并將每一行附加到字典中。
uj5u.com熱心網友回復:
我發現它有效的解決方案是下面的解決方案。我還需要根據表的長度增加 [i],以免覆寫資料。
var IntegrationDetails = []
for(let i=0;i < document.querySelectorAll('mat-row').length; i ) {
IntegrationDetails.push( {
"Property Name": document.querySelectorAll('mat-row')[i].querySelectorAll("mat-cell")[1].innerText,
"Advertiser": document.querySelectorAll('mat-row')[i].querySelectorAll("mat-cell")[2].innerText,
"Tracking ID" : document.querySelectorAll('mat-row')[i].querySelectorAll("mat-cell")[3].innerText,
"Account" : document.querySelectorAll('mat-row')[i].querySelectorAll("mat-cell")[4].innerText,
"Organisation name" : document.querySelectorAll('mat-row')[i].querySelectorAll("mat-cell")[5].innerText,
"Int with report data" : document.querySelectorAll('mat-row')[i].querySelectorAll("mat-cell")[6].innerText,
"Int with Cost Data" : document.querySelectorAll('mat-row')[i].querySelectorAll("mat-cell")[7].innerText,
"Int with Remarketing Lists" : document.querySelectorAll('mat-row')[i].querySelectorAll("mat-cell")[8].innerText
})
}
copy(IntegrationDetails)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/416190.html
標籤:
上一篇:在發布模式下,NewtonSoft不會將json反序列化為物件
下一篇:如何檢測是否所有復選框都被禁用?
