gpt4 book ai didi

javascript - 错误 : Evaluation Failed: ReferenceError: util is not defined

转载 作者:行者123 更新时间:2023-11-30 14:42:34 24 4
gpt4 key购买 nike

我试图将 util 模块对象传递给 puppeteer page.evaluate 但没有成功。我知道这个问题是在 How to pass required module object to puppeteer page.evaluate 中提出的但提供的解决方案不适用于我的情况。 MWE:

const puppeteer = require("puppeteer");

const path = "http://books.toscrape.com/";

// scrape funs
(async () =>{
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.goto(path, {waitUntil: "networkidle2", timeout: 0});
await page.waitFor(1000);
await page.addScriptTag({path: './node_modules/util/util.js'});
// selector with replaceable element
const buttonText = await page.evaluate(() => {
let selectorButton = "#default > div > div > div > div > section > div:nth-child(2) > ol > li:nth-child(%s) > article > div.product_price > form > button";
let buttons = [];
for(let i = 1; i < 21; i ++){
let textOut = document.querySelector(util.format(selectorButton, i)).innerText;
buttons.push(textOut);
};
return buttons;
});

// return
await browse.close();
console.log(buttonText);
})();

显示错误:

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Evaluation failed: ReferenceError: util is not defined

谢谢

编辑 1

在初始行中添加 const util = require("util"); 并且不起作用,如 How to pass required module object to puppeteer page.evaluate 所示.

编辑 2

即使我使用 browserify,我似乎也无法将 util 模块注入(inject) puppeteer 页面。步骤:

在项目 PATH 中,创建 main.js 如下:var util = require('util');

然后在终端的 PATH 上:browserify main.js -o bundle.js。文件 bundle.js 出现在项目 PATH 中。

然后运行以下命令:

const puppeteer = require("puppeteer");
const path = "http://books.toscrape.com/";

// scrape funs
(async () =>{
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.goto(path, {waitUntil: "networkidle2", timeout: 0});
await page.waitFor(1000);
await page.addScriptTag({path: "main.js"});
await page.addScriptTag({path: "bundle.js"});
// selector with replaceable element
const buttonText = await page.evaluate(() => {
let buttons = [];
let selectorButton = "#default > div > div > div > div > section > div:nth-child(2) > ol > li:nth-child(%s) > article > div.product_price > form > button";
for(let i = 1; i < 21; i ++){
let textOut = document.querySelector(util.format(selectorButton, i)).innerText;
buttons.push(textOut);
};
return buttons;
});

// return
await browse.close();
console.log(buttonText);
})();

错误:

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Evaluation failed: TypeError: Cannot read property 'format' of undefined at :5:55

最佳答案

您需要包含 ./node_modules/util/util.js 的浏览器兼容版本。您可以使用 browserify 或使用他们的在线服务 Browserify Wizard - util下载浏览器版本。

https://try-puppeteer.appspot.com/ 上的代码

const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto("http://books.toscrape.com/", {waitUntil: "networkidle2", timeout: 0});
await page.waitFor(1000);

//Copy of https://wzrd.in/standalone/util@latest
await page.addScriptTag({url: "https://cdn.rawgit.com/brahma-dev/099d0d6d43a5d013603bcd245ee7a862/raw/b0c6bb82905b5b868c287392000dc2487c41994d/util.js"});

// selector with replaceable element
const buttonText = await page.evaluate(() => {
let buttons = [];
let selectorButton = "#default > div > div > div > div > section > div:nth-child(2) > ol > li:nth-child(%s) > article > div.product_price > form > button";
for(let i = 1; i < 21; i ++){
let textOut = document.querySelector(util.format(selectorButton, i)).innerText;
buttons.push(textOut);
};
return buttons;
});

// return
await browser.close();
console.log(buttonText);

关于javascript - 错误 : Evaluation Failed: ReferenceError: util is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49429488/

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