gpt4 book ai didi

javascript - 使用 Async/Await 和 Puppeteer 映射数组以返回结果

转载 作者:行者123 更新时间:2023-12-03 00:55:49 26 4
gpt4 key购买 nike

我是异步编程的新手,因此我可能在这里遗漏了一些简单的东西。

我有一个 Express 项目,我在请求正文中传递一个数组。

在我的函数中,我验证主体,然后解析数组并在映射数组时使用 promise 。

const games = JSON.parse(JSON.stringify(req.body.games));

const gamesMap = games.map((game) => gameSearch(game));

return Promise.all(gamesMap)
.then(function(g) {
// async is still running here, I want to wait until it returns
console.log(g); // returns [ undefined, undefined, ... ]
});

游戏搜索功能使用puppeteer使用 headless 浏览器返回数组中传递的游戏价格。但是,它不会等到数组返回后再调用 Promise.all,因此上面的 console.log(g); 返回一个未定义的数组。我认为这与在 gameSearch 函数中使用 async wait 有关,尽管我不确定我应该在这里做什么?任何帮助将不胜感激。

function gameSearch(game) {
(async () => {
const url = '.....' + game;
try {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36');
await page.goto(url);
const selector = '.searchRcrd';
await page.waitForSelector(selector);

const searchRcrds = await page.$$(selector);

const records = [];

for (let i = 0; i < searchRcrds.length; i++) {
const searchRcrd = searchRcrds[i];
const title = await searchRcrd.$eval('h1', (h1) => h1.innerText.trim());
const buyFor = await searchRcrd.$eval('.desc .prodPrice div:nth-child(2) .priceTxt:nth-child(1)', (buy) => buy.innerText.trim());
const inStoreFor = await searchRcrd.$eval('.desc .priceTxt:nth-child(2)', (inStore) => inStore.innerText.trim());
const imgSrc = await searchRcrd.$eval('div.thumb > a > img', (img) => img.src.trim());

records.push({
'title': title,
'buyFor': buyFor,
'inStoreFor': inStoreFor,
'imgSrc': imgSrc
});
}

await browser.close();

return records;
} catch (err) {
next(err);
}
})();
}

最佳答案

返回记录(async () => {…})(); IIFE返回。删除它并使 gameSearch 本身成为一个返回( promise )数组的异步函数

async function gameSearch(game) {
const url = '.....' + game;
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36');
await page.goto(url);
const selector = '.searchRcrd';
await page.waitForSelector(selector);

const searchRcrds = await page.$$(selector);

const records = [];

for (let i = 0; i < searchRcrds.length; i++) {
const searchRcrd = searchRcrds[i];
const title = await searchRcrd.$eval('h1', (h1) => h1.innerText.trim());
const buyFor = await searchRcrd.$eval('.desc .prodPrice div:nth-child(2) .priceTxt:nth-child(1)', (buy) => buy.innerText.trim());
const inStoreFor = await searchRcrd.$eval('.desc .priceTxt:nth-child(2)', (inStore) => inStore.innerText.trim());
const imgSrc = await searchRcrd.$eval('div.thumb > a > img', (img) => img.src.trim());

records.push({
'title': title,
'buyFor': buyFor,
'inStoreFor': inStoreFor,
'imgSrc': imgSrc
});
}

await browser.close();

return records;
}

关于javascript - 使用 Async/Await 和 Puppeteer 映射数组以返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52843983/

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