【问题标题】:Is puppeteer supplying real time datapuppeteer 是否提供实时数据
【发布时间】:2021-02-08 21:08:09
【问题描述】:

我正在尝试在网络上抓取每个分数变化的实时分数。傀儡师能做到吗?如果可以的话,我应该在这段代码中添加什么,以便它返回实时数据。

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('site to go');
  await page.waitForSelector('input[name="username"]');
  await page.type('input[name="username"]', 'username');
  await page.type('input[name="password"]', 'password');
  await page.click('button[type="submit"]');
  let score = await page.evaluate(() => document.getElementById("scores").innerHTML);
})();

【问题讨论】:

    标签: javascript node.js puppeteer webautomation


    【解决方案1】:

    你可以使用exposeFunction注册一个回调函数:

    await page.exposeFunction('newScore', s => console.log(s));
    

    然后您可以在DOMSubtreeModified 事件上调用该函数:

    page.evaluate(() => document.getElementById('scores')
      .addEventListener('DOMSubtreeModified', () => newScore(element.innerHTML)));
    

    【讨论】:

    • 每10秒返回一次数据怎么样
    • @Newboy11 您可以使用setInterval 并在其中调用 evaluate 来解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-14
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    • 2010-09-14
    • 1970-01-01
    • 2021-10-01
    相关资源
    最近更新 更多