首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >木偶师:会议结束。很可能页面已经关闭了。

木偶师:会议结束。很可能页面已经关闭了。
EN

Stack Overflow用户
提问于 2020-10-21 21:44:28
回答 1查看 9.8K关注 0票数 3

每隔一页页面都会被挑剔,并出现以下错误:

代码语言:javascript
运行
复制
UnhandledPromiseRejectionWarning: Error: Protocol error (Runtime.callFunctionOn): Session closed. Most likely the page has been closed.
    at CDPSession.send (/Users/lancepollard/start/lancejpollard/data/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:195:35)
    at ExecutionContext._evaluateInternal (/Users/lancepollard/start/lancejpollard/data/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:200:50)
    at ExecutionContext.evaluate (/Users/lancepollard/start/lancejpollard/data/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:106:27)
    at DOMWorld.evaluate (/Users/lancepollard/start/lancejpollard/data/node_modules/puppeteer/lib/cjs/puppeteer/common/DOMWorld.js:79:24)
    at emitUnhandledRejectionWarning (internal/process/promises.js:149:15)
    at processPromiseRejections (internal/process/promises.js:211:11)
    at processTicksAndRejections (internal/process/task_queues.js:98:32)
(node:38857) Error: Protocol error (Runtime.callFunctionOn): Session closed. Most likely the page has been closed.
    at CDPSession.send (/Users/lancepollard/start/lancejpollard/data/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:195:35)
    at ExecutionContext._evaluateInternal (/Users/lancepollard/start/lancejpollard/data/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:200:50)
    at ExecutionContext.evaluate (/Users/lancepollard/start/lancejpollard/data/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:106:27)
    at DOMWorld.evaluate (/Users/lancepollard/start/lancejpollard/data/node_modules/puppeteer/lib/cjs/puppeteer/common/DOMWorld.js:79:24)
(node:38857) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

为什么会发生这种情况,你是如何解决的?

EN

Stack Overflow用户

回答已采纳

发布于 2020-11-02 07:22:32

我不知道你为什么会有这个错误,你应该把你的代码添加到这个问题上。

对我来说,这就是我处理压碎浏览器/页面错误的方法:

代码语言:javascript
运行
复制
async initiate() {
    this.pageOptions = {
        waitUntil: 'networkidle2',
        timeout: 60000
    };
    puppeteerExtra.use(pluginStealth());
    this.browser = await puppeteerExtra.launch({ headless: false });
    const browserWSEndpoint = await this.browser.wsEndpoint();
    puppeteerExtra.connect({ browserWSEndpoint: browserWSEndpoint });
    this.page = await this.browser.newPage();
    await this.page.setRequestInterception(true);
    this.page.on('request', (request) => {
        if (['image', 'stylesheet', 'font', 'script'].indexOf(request.resourceType()) !== -1) {
            request.abort();
        } else {
            request.continue();
        }
    });
    this.page.on('dialog', async dialog => {
        await dialog.dismiss();
    });
}

wait = (ms) => new Promise(resolve => setTimeout(resolve, ms))

async restart() {
    await this.close();
    await this.wait(1000);
    this.initiate();
}

async close() {
    if (this.browser) {
        await this.page.close();
        await this.browser.close();
        this.browser = null;
        this.page = null;
        this.pageOptions = null;
    }
}

爬行过程:

代码语言:javascript
运行
复制
 crawl(link, userAgent) {
    return new Promise(async (resolve, reject) => {
        if (reject) { }
        // Limit the runtime of this function in case of stuck URL crawling process.
        setTimeout(async () => {
            await this.restart();
            resolve(null);
            return;
        }, 60000);
        if (!userAgent) {
            userAgent = crawlUtils.getRandomUserAgent();
        }
        const crawlResults = { isValidPage: true, pageSource: null };
        if (!this.page) {
            await this.wait(1000);
            resolve(null);
            return;
        }
        try {
            await this.page.setUserAgent(userAgent);
            await this.page.goto(link, this.pageOptions);
            await this.page.waitForFunction(this.waitForFunction);
            crawlResults.pageSource = await this.page.content();
        }
        catch (error) {
            crawlResults.isValidPage = false;
        }
        if (this.isLinkCrawlTest) {
            await this.close();
        }
        resolve(crawlResults);
    });
}

希望它能帮助你解决问题。

票数 3
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64472147

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档