puppeteer 获取某个class下a标签 的href
时间: 2023-02-06 11:05:30 浏览: 272
使用 Puppeteer 获取某个 class 下的 a 标签的 href 可以这样做:
1. 先使用 `page.$$('.className a')` 来获取 class 为 `className` 的元素下的所有 a 标签。这会返回一个包含 a 标签的 ElementHandle 数组。
2. 然后遍历这个数组,对于每个 a 标签,使用 `page.$eval` 方法来获取它的 href 属性。例如:
```
const links = await page.$$('.className a');
for (const link of links) {
const href = await page.evaluate(el => el.getAttribute('href'), link);
console.log(href);
}
```
注意: 上面的代码中使用了异步操作,因此需要使用 `await` 关键字来等待结果。
希望这能帮到你!
阅读全文
相关推荐











