Node.js 使用傀儡机刮取动态站点
我试图建立一个简单的刮刀,将刮网站。 我想获得用户的徽章和点数 所以我用cheerio和木偶演员来完成这个任务 这是我的代码--> profile参数目前还没有被使用,因为我只是在测试它 问题: 每当我运行这段代码时,我不会在控制台上打印任何内容,如果我尝试不使用Puppeter,那么我只会得到没有任何数据的html。 我的预期结果是徽章和积分的数量 让我知道这个代码有什么问题Node.js 使用傀儡机刮取动态站点,node.js,salesforce,puppeteer,cheerio,Node.js,Salesforce,Puppeteer,Cheerio,我试图建立一个简单的刮刀,将刮网站。 我想获得用户的徽章和点数 所以我用cheerio和木偶演员来完成这个任务 这是我的代码--> profile参数目前还没有被使用,因为我只是在测试它 问题: 每当我运行这段代码时,我不会在控制台上打印任何内容,如果我尝试不使用Puppeter,那么我只会得到没有任何数据的html。 我的预期结果是徽章和积分的数量 让我知道这个代码有什么问题 谢谢一切都是正确的。您所要做的就是wait您的getPage调用,因为它是异步的。试试这个 .get("/:profi
谢谢一切都是正确的。您所要做的就是
wait
您的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)
.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());
});