从DOM中提取值

时间:2019-05-29 19:06:32

标签: html getelementsbyclassname inspect

Q1)如何使用FindElement / By类在XML / HTML中将其设置为“ 1”?

<h3 class="lemon--h3__373c0__sQmiG heading--h3__373c0__1n4Of alternate__373c0__1uacp">1<!-- -->.&nbsp;<a class="lemon--a__373c0__IEZFH link__373c0__29943 link-color--blue-dark__373c0__1mhJo link-size--inherit__373c0__2JXk5" href="/biz/teds-montana-grill-larimer-square-denver?osq=Teds+Montana+Grill" target="" name="Ted’s Montana Grill - Larimer Square" rel="">Ted’s Montana Grill - Larimer Square</a></h3>

Q2)如何从下面的xml中获取Ted's Montana GrillAurora? 我设法得到Ted’s Montana Grill。但是,事实证明Aurora是一个挑战。

以下是网址-https://www.yelp.com/biz/teds-montana-grill-aurora-aurora?osq=Teds+Montana+Grill

<h1 class="biz-page-title embossed-text-white">Ted’s Montana Grill -</h1>
<div class="u-inline-block">
            <h1 class="biz-page-title embossed-text-white">Aurora</h1>
            <div class="u-inline-block">

尝试使用FindElement /按

2 个答案:

答案 0 :(得分:0)

第一季度:

document.getElementsByClassName("lemon--h3__373c0__sQmiG heading--h3__373c0__1n4Of alternate__373c0__1uacp");

第二季度:

let items = document.getElementsByClassName("biz-page-title embossed-text-white");
for (let i = 0; i < items.length; i++) {
    console.log(items[i].outerText);
}

答案 1 :(得分:0)

2)它们都具有相同的类,因此您可以迭代getElementsByClassName返回的所有节点。您需要做的就是访问textContent属性。

const titles = document.getElementsByClassName("biz-page-title");
Array.prototype.forEach.call(titles, function(el) {
    console.log(el.textContent);
})

1)第一个问题也一样:const value = document.getElementsByClassName("lemon--h3__373c0__sQmiG heading--h3__373c0__1n4Of alternate__373c0__1uacp")[0].textContent;