我正在尝试构建一个简单的刮刀来抓取 Trailblazer Profile地点。 我想获取用户的徽章数和积分数。
所以我使用 Cheerio 和 puppeteer 来完成此任务。
这是我的代码-->
.get("/:profile", (req,res,next) => {
const url = "https://trailblazer.me/id/hverma99";
async function getPage(url) {
const browser = await puppeteer.launch({headless: true});
const page = await browser.newPage();
await page.goto(url, {waitUntil: 'networkidle0'});
const html = await page.content(); // serialized HTML of page DOM.
await browser.close();
return html;
}
const html = getPage(url);
const $ = cheerio.load(html);
const span = $('.tds-tally__count.tds-tally__count_success');
console.log(span.text());
});
目前尚未使用配置文件参数,因为我只是在测试它。
Problem : Whenever I run this code I do not get anything printed on the console and if i try without puppeteer then I only get the html without any data. My expected result is the number of badges and points.
让我知道这段代码有什么问题。
谢谢
最佳答案
一切都是正确的。您所要做的就是 await
您的 getPage
调用,因为它是异步的。试试这个
.get("/:profile", async (req,res,next) => {
const url = "https://trailblazer.me/id/hverma99";
async function getPage(url) {
const browser = await puppeteer.launch({headless: true});
const page = await browser.newPage();
await page.goto(url, {waitUntil: 'networkidle0'});
const html = await page.content(); // serialized HTML of page DOM.
await browser.close();
return html;
}
const html = await getPage(url);
const $ = cheerio.load(html);
const span = $('.tds-tally__count.tds-tally__count_success');
console.log(span.text());
});
还需要像这样放置async
- async (req,res,next)
关于node.js - 使用 puppeteer 抓取动态站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60589915/