簡體   English   中英

使用帶有Puppeteer的Async / Await在數組上進行映射以返回結果

[英]Mapping over array using Async/Await with Puppeteer to return result

我是異步編程的新手,所以這里可能缺少一些簡單的東西。

我有一個快遞項目,我在請求的正文中傳遞了一個數組。

在函數內部,我驗證主體,然后解析數組,並在映射到數組上時使用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者使用無頭瀏覽器來返回以數組形式傳遞的游戲價格。 但是,它不會等到返回數組之后Promise.all調用Promise.all ,因此console.log(g); 上面返回一個未定義的數組。 我想這與在gameSearch函數中使用async await有關,盡管我不確定在這里應該做什么? 任何幫助將不勝感激。

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);
    }
  })();
}

return records(async () => {…})(); IIFE。 刪除它並使gameSearch本身成為一個async function ,該async function返回數組(對數組的承諾)。

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM