javascript - Puppeteer - 评估方法中的异步函数引发错误

标签 javascript node.js web-scraping puppeteer

我正在尝试检查 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();

最佳答案

evaluate 中的所有代码都在 chromium 端执行。
由于 urlExists 是在 Node 端导入的,因此您无法从浏览器访问该函数。

除非您使用 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()

关于javascript - Puppeteer - 评估方法中的异步函数引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57908630/

相关文章:

javascript - SequelizeEagerLoading错误: parent model is not associated to child

python - 试图在一个 div 中抓取一个 div 中的元素,无法弄清楚

python - 如何使用 requests python 模块登录 fidelity.com

javascript - 表单验证码不起作用

javascript - Apollo makeExecutableSchema 抛出错误 "Cannot read property ' 种“未定义”

c# - 在 Node.js 中调用 C# 代码时出现意外结果

python - 如何在 Python 中使用 BeautifulSoup 在文本字符串后查找表格?

javascript - 是否可以在 jQuery 中添加 OR 语句

javascript - 如何限制列表父 div 的高度并使内容可滚动?

mysql - 我需要担心使用 sequelize 的 node.js 的 mysql 连接池吗?