【问题标题】:Puppeteer - Async function in evaluate method throws errorPuppeteer - 评估方法中的异步函数引发错误
【发布时间】:2019-09-12 14:12:02
【问题描述】:

我正在尝试检查og:image 源是否存在。如果我想在evaluate 函数中调用异步方法,我会得到Error: Evaluation failed: [object Object] 错误。

    Error: Evaluation failed: [object Object]
    at ExecutionContext._evaluateInternal (.../node_modules/puppeteer/lib/ExecutionContext.js:122:13)
    at processTicksAndRejections (internal/process/task_queues.js:89:5)
    at async ExecutionContext.evaluate (.../node_modules/puppeteer/lib/ExecutionContext.js:48:12)
  -- ASYNC --
    at ExecutionContext.<anonymous> (.../node_modules/puppeteer/lib/helper.js:111:15)
    at DOMWorld.evaluate (.../node_modules/puppeteer/lib/DOMWorld.js:112:20)
    at processTicksAndRejections (internal/process/task_queues.js:89:5)
  -- ASYNC --
    at Frame.<anonymous> (.../node_modules/puppeteer/lib/helper.js:111:15)
    at Page.evaluate (.../node_modules/puppeteer/lib/Page.js:827:43)
    at Page.<anonymous> (.../node_modules/puppeteer/lib/helper.js:112:23)
    at run (/Users/andrejgajdos/devel/link-preview/app.js:195:28)
    at processTicksAndRejections (internal/process/task_queues.js:89:5)

app.sj

const puppeteer = require("puppeteer");
const util = require('util');
const urlExists = util.promisify(require('url-exists'));

const run = async () => {
  try {
    const browser = await puppeteer.launch({ headless: true });
    const page = await browser.newPage();

    await page.goto('https://www.onepeloton.com/', { waitUntil: 'domcontentloaded' });

    const img = await page.evaluate(async () => {
      const ogImg = document.querySelector('meta[property="og:image"]');
      if (ogImg != null && ogImg.content.length > 0) {
        const isExists = await urlExists(ogImg.content);
        return isExists;
      }
    });
    console.log(img);

    await browser.close()
  } catch (e) {
    console.log(e);
  }
}

run();

【问题讨论】:

    标签: javascript node.js web-scraping puppeteer


    【解决方案1】:

    evaluate 中的所有代码都在 Chromium 端执行。
    由于urlExists 正在节点端导入,您将无法从浏览器访问该功能。

    除非您使用page.exposeFunction 公开它。一旦你暴露了这个函数,chromium 就可以调用urlExists

    const browser = await puppeteer.launch({ headless: true });
    const page = await browser.newPage();
    
    await page.goto('https://www.onepeloton.com/', { waitUntil: 'domcontentloaded' });
    await page.exposeFunction('urlExists', urlExists);
    
    const img = await page.evaluate(async () => {
        const ogImg = document.querySelector('meta[property="og:image"]');
        if (ogImg != null && ogImg.content.length > 0) {
        const isExists = await urlExists(ogImg.content);
        return isExists;
        }
    });
    console.log(img);
    
    await browser.close()
    

    【讨论】:

    • Manu 感谢@hardkoded,让我知道 page.exposeFunction。你为我节省了很多时间。
    猜你喜欢
    • 1970-01-01
    • 2019-05-23
    • 2021-12-19
    • 2012-09-20
    • 1970-01-01
    • 2018-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多