gpt4 book ai didi

javascript - 异步函数抛出错误,但我看不到它们

转载 作者:太空宇宙 更新时间:2023-11-03 23:13:59 25 4
gpt4 key购买 nike

我是异步函数甚至 Node.js 的新手。我正在尝试抓取一个网站并从中获取一些数据。当抓取网站时,我的代码随机抛出一个丑陋的错误:

    (node:6128) UnhandledPromiseRejectionWarning: Error: Evaluation failed: TypeError: Cannot read property 'innerText' of null
at __puppeteer_evaluation_script__:2:75
at ExecutionContext._evaluateInternal (c:\projects\ultipro-crawler\node_modules\puppeteer\lib\ExecutionContext.js:122:13)
at process._tickCallback (internal/process/next_tick.js:68:7)
-- ASYNC --
at ExecutionContext.<anonymous> (c:\projects\ultipro-crawler\node_modules\puppeteer\lib\helper.js:111:15)
at DOMWorld.evaluate (c:\projects\ultipro-crawler\node_modules\puppeteer\lib\DOMWorld.js:112:20)
at process._tickCallback (internal/process/next_tick.js:68:7)
-- ASYNC --
at Frame.<anonymous> (c:\projects\ultipro-crawler\node_modules\puppeteer\lib\helper.js:111:15)
at Page.evaluate (c:\projects\ultipro-crawler\node_modules\puppeteer\lib\Page.js:827:43)
at Page.<anonymous> (c:\projects\ultipro-crawler\node_modules\puppeteer\lib\helper.js:112:23)
at c:\projects\ultipro-crawler\phantom.js:21:30
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:6128) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:6128) [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.

在错误的底部,我可以看到它告诉我我有一个未经处理的 promise 拒绝,但我不知道如何更正我的代码来解决这个问题。我对使用 Promise 和异步函数不熟悉,似乎无法在这里找到一般错误。

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

(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();

const oportunities = [];
const opportunity = {
title: '',
desc: '',
category: '',
reqName: '',
hours: '',
postingDate: '',
address: ''
};

await page.goto('https://recruiting2.ultipro.com/PUB1004PSCU/JobBoard/d433f5c3-37c8-4bcf-a3af-248a707c7d31/?q=&o=postedDateDesc');

const title = await page.evaluate(() => {
return document.querySelector('.opportunity .row .col-lg-20 h3 a').innerText
});
const desc = await page.evaluate(() => {
return document.querySelector('.opportunity .hidden-xs.paragraph').innerText
});

opportunity.title = title;
opportunity.desc = desc;


console.log(opportunity);

browser.close();
})();

最佳答案

您无法从异步 IIFE 的外部范围捕获错误。要么尝试/捕获 IIFE 中的所有代码:

(async () => {
try {
const browser = await puppeteer.launch();
const page = await browser.newPage();

const oportunities = [];
const opportunity = {
title: '',
desc: '',
category: '',
reqName: '',
hours: '',
postingDate: '',
address: ''
};

await page.goto('https://recruiting2.ultipro.com/PUB1004PSCU/JobBoard/d433f5c3-37c8-4bcf-a3af-248a707c7d31/?q=&o=postedDateDesc');

const title = await page.evaluate(() => {
return document.querySelector('.opportunity .row .col-lg-20 h3 a').innerText
});
const desc = await page.evaluate(() => {
return document.querySelector('.opportunity .hidden-xs.paragraph').innerText
});

opportunity.title = title;
opportunity.desc = desc;


console.log(opportunity);

browser.close();
} catch (e) {
console.error(e)
}
})();

在顶级范围内使用 promise :

async function doJob() {
const browser = await puppeteer.launch();
const page = await browser.newPage();

const oportunities = [];
const opportunity = {
title: '',
desc: '',
category: '',
reqName: '',
hours: '',
postingDate: '',
address: ''
};

await page.goto('https://recruiting2.ultipro.com/PUB1004PSCU/JobBoard/d433f5c3-37c8-4bcf-a3af-248a707c7d31/?q=&o=postedDateDesc');

const title = await page.evaluate(() => {
return document.querySelector('.opportunity .row .col-lg-20 h3 a').innerText
});
const desc = await page.evaluate(() => {
return document.querySelector('.opportunity .hidden-xs.paragraph').innerText
});

opportunity.title = title;
opportunity.desc = desc;


console.log(opportunity);

browser.close();
}

doJob()
.catch(console.error)

或者从另一个函数调用您的函数:

async function doJob () {
const browser = await puppeteer.launch();
const page = await browser.newPage();

const oportunities = [];
const opportunity = {
title: '',
desc: '',
category: '',
reqName: '',
hours: '',
postingDate: '',
address: ''
};

await page.goto('https://recruiting2.ultipro.com/PUB1004PSCU/JobBoard/d433f5c3-37c8-4bcf-a3af-248a707c7d31/?q=&o=postedDateDesc');

const title = await page.evaluate(() => {
return document.querySelector('.opportunity .row .col-lg-20 h3 a').innerText
});
const desc = await page.evaluate(() => {
return document.querySelector('.opportunity .hidden-xs.paragraph').innerText
});

opportunity.title = title;
opportunity.desc = desc;


console.log(opportunity);

browser.close();
}

(async () => {
try {
await doJob()
} catch (err) {
console.error(err)
}
})()

这些是我能想到的 atm 的一些选项:)

关于javascript - 异步函数抛出错误,但我看不到它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57855893/

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