node.js - 使用 puppeteer 抓取动态站点

标签 node.js salesforce puppeteer cheerio

我正在尝试构建一个简单的刮刀来抓取 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/

相关文章:

html - 查询字符串中的查询字符串

cloud-foundry - 将 Google Chrome Puppeteer 应用程序推送到 Predix 时出错

javascript - 未声明 HTML 文档的字符编码

node.js - Google Cloud 跟踪 自定义跟踪只能运行几次

javascript - Html/CSS 星级反馈

javascript - 返回并使用浏览器的后退按钮显示先前的值

javascript - Puppeteer - page.$$ ('' ).length 返回未定义

javascript - 如何使用 puppeteer 通过占位符选择元素

node.js - 502 Bad Gateway - Ubuntu 16.04.2 LTS 上的 Meteor 和 nginx

eclipse - nodeclipse 一次只允许调试 1 个 .js 文件