gpt4 book ai didi

javascript - 带有来自 Promise Async 的 puppeteer 错误的 NodeJS

转载 作者:行者123 更新时间:2023-11-30 14:07:37 27 4
gpt4 key购买 nike

我正在使用 NodeJS 开发社区自动屏幕捕获应用程序。实际上,它与它的屏幕截图功能配合得很好当我等了一会儿它似乎有一个错误并且不再工作了。

我在控制台中发现了这样的错误消息。

(node:23436) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'screenshot' of null
at C:\Users\user\Downloads\Projects\Bander-Statistics\google.js:60:31
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:23436) 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:23436) [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.
same data

我认为问题出在异步函数上。但是我必须等待很长时间才能再次看到错误消息,我不知道为什么会这样。

这是我的完整代码

const puppeteer = require('puppeteer');
const cheerio = require('cheerio')
const request = require('request');

let postArray = [];
let uniq = [];
let initNumber;
let prevNumber;

function data() {
(async () => {
await request.get({
url: 'https://www.clien.net/service/group/community'
}, function (err, response, body) {
if (err) {
throw err;
}
let $ = cheerio.load(body);
initNumber = $('.list_item.symph_row').attr('data-board-sn');
})
if (initNumber === prevNumber) {
console.log('same data'); // Same Data, Do nothing
// prevNumber = initNumber; // Compare with prevNumber
} else {
postArray.push(initNumber); // Different Data put to Array
console.log(postArray);
prevNumber = initNumber; // Compare with prevNumber
}
uniq = postArray.reduce(function (a, b) { // Sometimes in postArray have duplicate value so remove it.
if (a.indexOf(b) < 0) a.push(b);
return a;
}, []);
console.log(uniq);
})()
}

setInterval(data, 1000); // Interval 1 second because new post's are really uploaded fast

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

// Adjustments particular to this page to ensure we hit desktop breakpoint.
await page.setViewport({
width: 1920,
height: 100000,
deviceScaleFactor: 1
});
if (uniq.length === 0) { // If postArray are 0, Do nothing
console.log(`This time Array ${uniq.length}`)
console.log('No Processing this time');
} else {

for (let i = 0; i < uniq.length; i++) { //If postArray have value iterate all value to screenshot
await page.goto(`https://www.clien.net/service/board/park/${uniq[i]}`);
const overlay = await page.$('.post_view');
await overlay.screenshot({
path: `./image/${uniq[i]}.png`, // save file name with postarray value
}).catch((err) => console.log(`Processing Image Clear Array INIT again ${err}`)) // Browser Close)
}
postArray = []; // Reset Array
await browser.close();
}
postArray = []; // Reset Array
await browser.close(); // Browser Close
})();
}

setInterval(screenshot, 30000); // Interval 10 seconds

最佳答案

我敢打赌问题是您无法获得 .post_view。这就是为什么它为空。您可以尝试在那里使用 waitForSelector

await page.goto(`https://www.clien.net/service/board/park/${uniq[i]}`);
const overlay = await page.waitForSelector('.post_view').catch((err) => console.log('Unable to get a post_view element'));
if (overlay)
await overlay.screenshot({
path: `./image/${uniq[i]}.png`, // save file name with postarray value
}).catch((err) => console.log(`Processing Image Clear Array INIT again ${err}`)) // Browser Close)

关于javascript - 带有来自 Promise Async 的 puppeteer 错误的 NodeJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55098230/

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