我是 javascript 新手,正在嘗試運行 javascript 函式并在下面回傳對此網站的回應。我正在嘗試使用“document.querySelectorAll”來回傳所有職位的回應以及每個職位發布的 URL。如何回傳對這些元素的回應?
網站:https : //mckesson.wd3.myworkdayjobs.com/External_Careers
這是我最近的嘗試:
function ExecuteScript() {
let nameList = [];
let response = '';
document.querySelectorAll('a[data-automation-id="jobTitle"]').forEach((element, i) => {
nameList.push(element.innerHTML);
document.querySelectorAll('a[data-automation-id="jobTitle"]').forEach((element, i) => {
response = nameList[i] '\\t' element.getAttribute('hef') '\\n';
});
return response;
}
uj5u.com熱心網友回復:
示例代碼有幾個問題:
語法錯誤:兩個
querySelectorAll嵌套不正確。拼寫錯誤:
hef應該是href,只存盤相對地址是不夠的;您需要在它們前面添加域名。因為兩者都捕獲同一類的元素,所以可以合并兩個回圈。
可以參考以下代碼:
function ExecuteScript() {
let response = '';
document.querySelectorAll('a[data-automation-id="jobTitle"]').forEach((element, i) => {
response = element.innerHTML '\\t' location.host element.getAttribute('href') '\\n';
});
return response;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/341151.html
標籤:javascript 网页抓取
