【问题标题】:Scrape dynamic site using puppeteer使用 puppeteer 抓取动态站点
【发布时间】:2020-06-20 17:09:34
【问题描述】:

我正在尝试构建一个简单的刮板来刮掉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());

});

目前还没有使用 profile 参数,因为我只是在测试它。

问题: 每当我运行此代码时,我都不会在控制台上打印任何内容,如果我尝试不使用 puppeteer,那么我只会得到没有任何数据的 html。 我的预期结果是徽章和积分的数量。

让我知道这段代码有什么问题。

谢谢

【问题讨论】:

    标签: node.js salesforce puppeteer cheerio


    【解决方案1】:

    一切都是正确的。你所要做的就是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)

    【讨论】:

      猜你喜欢
      • 2020-07-02
      • 2019-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-21
      • 2010-09-17
      相关资源
      最近更新 更多