第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何獲取沒(méi)有 Id 或標(biāo)記名的 html 的文本內(nèi)容?

如何獲取沒(méi)有 Id 或標(biāo)記名的 html 的文本內(nèi)容?

慕哥9229398 2023-05-11 10:23:02
我正在開發(fā)一個(gè)供個(gè)人使用的 chrome 擴(kuò)展(使用 Javascript),它會(huì)抓取一些數(shù)據(jù)并將其導(dǎo)出到 csv 文件,現(xiàn)在我在提取一些文本節(jié)點(diǎn)時(shí)遇到問(wèn)題,因?yàn)樗鼈兊倪x擇器(chrome css 選擇器)會(huì)根據(jù)如何變化而變化存在許多具有相同類別但內(nèi)容不同的標(biāo)簽。這是 Html 的示例:<li class="sc-account-rows__row"><div class="sc-account-rows__row__label">RTF</div> // Title Label<div class="sc-account-rows__row__price--container"><div class="sc-account-rows__row__price">-$ 1.485</div> // Price Label <- How to get This?</div></li><li class="sc-account-rows__row"><div class="sc-account-rows__row__label">some text</div> // Another Label<div class="sc-account-rows__row__price--container"><div class="sc-account-rows__row__price">-$ 2.418</div> // Another price which I don't need but has same class</div></li>換句話說(shuō),這個(gè)特定標(biāo)簽的選擇器可以是:#root-app > div > div.sc-account-section > div.sc-account-section__container > div.sc-account-module > div:nth-child(3) > ul > li:nth-child(1) > div.sc-account-rows__row__price--container > div或者#root-app > div > div.sc-account-section > div.sc-account-section__container > div.sc-account-module > div:nth-child(3) > ul > li:nth-child(9) > div.sc-account-rows__row__price--container > divAs you can see there is no Id or Name assigned to this particular label, I was using (successfully) this piece of code when the selector was always the same. (注意這是在 iframe 中)var RTF_fee = "#root-app > div > div.sc-account-section > div.sc-account-section__container > div.sc-account-module > div:nth-child(3) > ul > li:nth-child(4) > div.sc-account-rows__row__price--container > div";    if (iframe_document.body.contains(iframe_document.querySelector(RTF_fee))) {        RTF_value = iframe_document.querySelector(RTF_fee).textContent;        console.log(RTF_value);    }    else {        RTF_value = "0";        console.log(RTF_value);    }所以,問(wèn)題是:如果我沒(méi)有唯一的選擇器/ID,如何獲取價(jià)格標(biāo)簽中的文本內(nèi)容?我想我可以處理價(jià)格標(biāo)簽類別始終為“sc-account-rows__row__price”并且價(jià)格之前的標(biāo)簽文本始終為“RTF”的事實(shí),但我不知道如何編碼或者是否有更好的選擇。我為我糟糕的“編程”語(yǔ)言道歉,我只是一個(gè)臨時(shí)程序員)提前致謝。
查看完整描述

2 回答

?
蝴蝶不菲

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊

如果我正確理解你的問(wèn)題,這就是你要找的。



let labels = document.querySelectorAll(".sc-account-rows__row__label");


[...labels].forEach(label => {

  if (label.innerText === "RTF") {

    let price = label.parentElement.querySelector(".sc-account-rows__row__price").innerText;

    console.log(price);

  }

})


Console: "-$ 1.485"

如果您需要使用除 RTF 之外的額外標(biāo)簽:


let words = ["RTF", "something", "else"];


// change if condition to

if (words.includes[label.innerText]) {...}


查看完整回答
反對(duì) 回復(fù) 2023-05-11
?
慕的地6264312

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超6個(gè)贊

另一種方法是使用 XPath,這是一種舊的 XML 導(dǎo)航標(biāo)準(zhǔn),也適用于 HTML。在本例中,您正在尋找一個(gè)類div為“sc-account-rows__row__price”的類,它div附近有另一個(gè)類為“sc-account-rows__row__label”且文本為“RTF”的類。

div 的 XPath 是

//div[@class="sc-account-rows__row__label"][text()="RTF"]/following-sibling::div[@class="sc-account-rows__row__price--container"]//div[@class="sc-account-rows__row__price"]

由于 price--container/price 的嵌套性質(zhì),它有點(diǎn)復(fù)雜div。

要獲取文本,您將使用document.evaluate

function getPriceByLabel(label) {

? // change this to a parent element if you can; it will speed up the process.

? const contextNode = document;

? const priceResults = document.evaluate(`//div[@class="sc-account-rows__row__label"][text()="${label}"]/following-sibling::div[@class="sc-account-rows__row__price--container"]//div[@class="sc-account-rows__row__price"]`, contextNode, null, XPathResult.ANY_UNORDERED_NODE_TYPE, null);

? const priceElement = priceResults.singleNodeValue;

? const price = priceElement.textContent;

? return price;

}

console.log(getPriceByLabel("RTF"));

console.log(getPriceByLabel("some text"));

<li class="sc-account-rows__row">

? <div class="sc-account-rows__row__label">RTF</div>

? <div class="sc-account-rows__row__price--container">

? ? <div class="sc-account-rows__row__price">-$ 1.485</div>

? </div>

</li>


<li class="sc-account-rows__row">

? <div class="sc-account-rows__row__label">some text</div>

? <div class="sc-account-rows__row__price--container">

? ? <div class="sc-account-rows__row__price">-$ 2.418</div>

? </div>

</li>

我把它放在一個(gè)帶有標(biāo)簽并吐出價(jià)格的函數(shù)中。



查看完整回答
反對(duì) 回復(fù) 2023-05-11
  • 2 回答
  • 0 關(guān)注
  • 226 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)