gpt4 book ai didi

javascript - 如何在puppeteer page.evaluate中使用url模块

转载 作者:行者123 更新时间:2023-11-30 09:16:20 25 4
gpt4 key购买 nike

我已经尝试了 Error: Evaluation Failed: ReferenceError: util is not defined 中提到的所有内容和 How to pass required module object to puppeteer page.evaluate 。具体来说,我尝试使用 browserify 转换 url.js(我还尝试将 url.js 和 punycode.js 一起转换),并将相应的脚本(bundle.js)添加到页面环境中。

我正在尝试在 puppeteer 中使用 page.evaluate() 内的 url 模块。这是一个显示错误的非常简单的示例:

const puppeteer = require('puppeteer');

puppeteer.launch({dumpio: true}).then(async browser => {
const page = await browser.newPage();
const response = await page.goto('https://www.google.com');
await page.waitFor(5000);
const pageUrl = page.url();
await page.addScriptTag({path: 'bundle.js'});
await page.evaluate(pageUrl => {
const anchors = Array.from(document.querySelectorAll('a'));
for (let anchor of anchors) {
const href = anchor.getAttribute('href');
let hrefUrl;
try {
hrefUrl = new URL(href);
} catch (e) {
hrefUrl = new URL(href, pageUrl);
}
console.log(url.format(hrefUrl, {fragment: false}));
}
}, pageUrl);
await page.close();
await browser.close();
});

此示例生成以下错误:

(node:23667) UnhandledPromiseRejectionWarning: Error: Evaluation failed: ReferenceError: url is not defined at pageUrl (puppeteer_evaluation_script:11:19) at ExecutionContext.evaluateHandle (/home/webb/node_modules/puppeteer/lib/ExecutionContext.js:97:13) at at process._tickCallback (internal/process/next_tick.js:188:7)

我还需要做什么才能让 url 模块被识别?

最佳答案

带有 page.exposeFunction() 的变体:

'use strict';

const url = require('url');
const puppeteer = require('puppeteer');

puppeteer.launch({ dumpio: true }).then(async browser => {
const page = await browser.newPage();
await page.exposeFunction('formatURL', formatURL);

const response = await page.goto('https://www.google.com');
await page.waitFor(5000);
const pageUrl = page.url();

await page.evaluate(async (pageUrl) => {
const anchors = Array.from(document.querySelectorAll('a'));
for (const anchor of anchors) {
const href = anchor.getAttribute('href');
const hrefUrl = await formatURL(href, pageUrl);
console.log(hrefUrl);
}
}, pageUrl);

await page.close();
await browser.close();
});

function formatURL(href, base) {
try {
return url.format(new URL(href), { fragment: false });
} catch (e) {
return url.format(new URL(href, base), { fragment: false });
}
}

关于javascript - 如何在puppeteer page.evaluate中使用url模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54855004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com