使用 Puppeteer 获取class为grid-header clearfix下的a标签值和链接
时间: 2023-02-06 10:12:36 浏览: 261
使用 Puppeteer 获取 class 为 grid-header clearfix 下的 a 标签值和链接,可以使用如下代码:
```
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
const elements = await page.$$('.grid-header.clearfix a');
for (const element of elements) {
const value = await element.getProperty('textContent');
const link = await element.getProperty('href');
console.log(await value.jsonValue(), await link.jsonValue());
}
await browser.close();
})();
```
在这段代码中,我们使用了 Puppeteer 的 `$$` 方法来获取页面中所有 class 为 `grid-header clearfix` 的 a 标签元素。然后,我们使用 `for` 循环来遍历这些元素,并使用 `getProperty` 方法获取它们的文本内容和链接地址,最后使用 `jsonValue` 方法将其转换为可以在 JavaScript 中使用的值。
阅读全文
相关推荐


















