gpt4 book ai didi

javascript - 在 puppeteer 中访问子元素

转载 作者:行者123 更新时间:2023-11-30 09:18:14 24 4
gpt4 key购买 nike

我有下面的 HTML 结构

<div class ="container" id= "12">
<div class="details" desc-type= "multiline">
<a href="#">
<div class="description"> Some Description </div>
</a>
</div>
</div>

我用下面的代码抓取了它

const SELECTOR =
"div.container";

const movies = await page.$$eval(
SELECTOR,
nodes =>
nodes.map(element => {
return {
movieID: element.getAttribute("id"),
};
} )
);

如何修改上面的代码以便我可以读取desc-type= "multiline"innerText<div class="description">

最佳答案

这个怎么样?

const movies = await page.$$eval(
SELECTOR,
nodes =>
nodes.map(element => {
return {
movieID: element.getAttribute("id"),
descType: element.querySelector('[desc-type]').getAttribute('desc-type'),
description: element.querySelector(".description").innerText
};
} )
);

关于javascript - 在 puppeteer 中访问子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53515775/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com