英文:
how to get readable value from CDPElementHandle
问题
I want to get actual text content inside the element with class 'example'. How to extract that value? I used .getProperties with .jsonValue, but it didn't work. Any help would be appreciated.
英文:
I am just trying to scrap something from some website my code looks like it
const puppeteer = require("puppeteer")
const main = async () => {
const browser = await puppeteer.launch({})
const page = await browser.newPage()
await page.goto("https://www.example.com")
await page.waitForSelector(".example")
const titleNode = await page.$$(".example")
titleNode.forEach( el => {
el.getProperties("textContent").then(el => {
console.log(el)
})
})
console.log( titleNode );
browser.close()
}
main()
And result is something like this
[
CDPElementHandle { handle: CDPJSHandle {} },
CDPElementHandle { handle: CDPJSHandle {} },
CDPElementHandle { handle: CDPJSHandle {} },
CDPElementHandle { handle: CDPJSHandle {} },
CDPElementHandle { handle: CDPJSHandle {} },
]
i want to get actual text content inside the element with class 'example'
how to extract that value i used .getProperties with .jsonValue but it didnt work
Any help would be appreciated
答案1
得分: 4
Array.prototype.forEach
不适用于异步代码,所以请改用 for...of
或 map
。
const puppeteer = require("puppeteer");
const html = `
<div>
<a>text1</a>
<a class='example'>text2</a>
<a>text3</a>
<a class='example'>text4</a>
<a>text5</a>
<a>text6</a>
</div>
`;
const main = async () => {
const browser = await puppeteer.launch({})
const page = await browser.newPage()
await page.setContent(html);
const titleNode = await page.$$(".example");
let result = [];
for(let t of titleNode) {
result.push(await t.evaluate(x => x.textContent));
}
let result2 = await Promise.all(titleNode.map(async (t) => {
return await t.evaluate(x => x.textContent);
}))
console.log({result : result, result2 : result2});
}
main();
英文:
Array.prototype.forEach
is not designed for asynchronous code, so instead of using .forEach
use for...of
, or map
.
Code :
const puppeteer = require("puppeteer");
const html = `
<div>
<a>text1</a>
<a class='example'>text2</a>
<a>text3</a>
<a class='example'>text4</a>
<a>text5</a>
<a>text6</a>
</div>
`;
const main = async () => {
const browser = await puppeteer.launch({})
const page = await browser.newPage()
await page.setContent(html);
const titleNode = await page.$$(".example");
let result = [];
for(let t of titleNode) {
result.push(await t.evaluate(x => x.textContent));
}
let result2 = await Promise.all(titleNode.map(async (t) => {
return await t.evaluate(x => x.textContent);
}))
console.log({result : result, result2 : result2});
}
main();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论