gpt4 book ai didi

javascript - Node Puppeteer,page.on( "request") 抛出 "Request is already handled!"

转载 作者:行者123 更新时间:2023-12-04 11:56:53 29 4
gpt4 key购买 nike

我正在使用 puppeteer-extra 和 node.js 遍历多个 url。
我试图拦截一些资源类型以在每次迭代时加载,并得到以下错误。

PS C:\Users\someuser\Desktop\Project> node temp.js
-- running
C:\Users\someuser\node_modules\puppeteer\lib\cjs\puppeteer\common\assert.js:26
throw new Error(message);
^

Error: Request is already handled!
at Object.exports.assert (C:\Users\someuser\node_modules\puppeteer\lib\cjs\puppeteer\common\assert.js:26:15)
at HTTPRequest.continue (C:\Users\someuser\node_modules\puppeteer\lib\cjs\puppeteer\common\HTTPRequest.js:217:21)
at PuppeteerBlocker.onRequest (C:\Users\someuser\node_modules\@cliqz\adblocker-puppeteer\dist\cjs\adblocker.js:225:33)
at BlockingContext.onRequest (C:\Users\someuser\node_modules\@cliqz\adblocker-puppeteer\dist\cjs\adblocker.js:64:47)
at C:\Users\someuser\node_modules\puppeteer\lib\cjs\vendor\mitt\src\index.js:51:62
at Array.map (<anonymous>)
at Object.emit (C:\Users\someuser\node_modules\puppeteer\lib\cjs\vendor\mitt\src\index.js:51:43)
at Page.emit (C:\Users\someuser\node_modules\puppeteer\lib\cjs\puppeteer\common\EventEmitter.js:72:22)
at C:\Users\someuser\node_modules\puppeteer\lib\cjs\puppeteer\common\Page.js:143:100
at C:\Users\someuser\node_modules\puppeteer\lib\cjs\vendor\mitt\src\index.js:51:62
我无法理解为什么该请求已作为实际请求处理 page.gotofor 中完成环形。有人会有任何提示吗?
这是完整的项目
const puppeteer = require( 'puppeteer-extra' );

const StealthPlugin = require( 'puppeteer-extra-plugin-stealth' );
puppeteer.use( StealthPlugin() );

const AdblockerPlugin = require( 'puppeteer-extra-plugin-adblocker' );
puppeteer.use( AdblockerPlugin( { blockTrackers: true } ) );

puppeteer.launch( { headless: true } ).then( async browser => {

console.log( '--\xa0running' );

console.time( '--\xa0process' );

const page = await browser.newPage();

await page.setRequestInterception( true );

page.on( 'request', ( request ) => {
if ( [ 'image', 'stylesheet', 'font', 'script' ].indexOf( request.resourceType() ) ) {
request.abort();
} else {
request.continue();
};
} );

for ( var i = 1; i <= 20; i++ ) {

console.time( '--\xa0iteration\xa0' + i ); // ... timer start

await page.goto( 'https://www.someurl.it/shop/s%2D' + i, { waitUntil: 'load' } );

const title = await page.title();

console.log( title.includes( '404' ) ? false : title );

console.timeEnd( '--\xa0iteration\xa0' + i ); // ... timer end

};

await browser.close();

console.timeEnd( '--\xa0process' );

console.log( '--\xa0ending' );

} );

最佳答案

添加退货声明解决了我的问题。

page.on( 'request', ( request ) => {
if ([ 'image', 'stylesheet', 'font', 'script' ].indexOf( request.resourceType() ) !== -1 ) {
return request.abort();
}
request.continue();
} );

关于javascript - Node Puppeteer,page.on( "request") 抛出 "Request is already handled!",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65922416/

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