【发布时间】:2020-05-12 17:43:43
【问题描述】:
以下问题类似,但答案无用:How to get children of elements by Puppeteer。在这些答案中,用户展示了如何获取元素的属性。我需要访问子节点列表。
在我正在抓取的页面中,有 30 个包含非常复杂且不同内容的框。
我的目标是
- 获取外部盒子 (DIVs)
- 检查是否存在一些特定的嵌套 html 元素
- 检索嵌套元素的内容
例如
- 第一个盒子有嵌套链接的图像
- 第二个盒子有 4 个<P>s
感谢answer to my previous question,我能够检索所有DIVs
我正在这样做
const boxes = await page.$$("DIV.a-row DIV.spinnerDeal DIV.box DIV.details")
boxes.forEach((item) => {
... now, for example how to check if the item contains a P with a specific class=
})
所以我的问题是(例如)如何检查项目是否包含具有特定类的 P。
更一般的问题是如何浏览每个项目的嵌套元素?
我试过了
boxes.forEach((item) => {
console.log(item.getChildNodes())
console.log("---")
})
但是getChildNodes() is not a function
【问题讨论】:
-
因此,根据您之前的问题,您应该在循环内使用评估函数。
标签: puppeteer