Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JS Puppeter使用for循环在链接上迭代_Javascript_Node.js_Puppeteer - Fatal编程技术网

Javascript JS Puppeter使用for循环在链接上迭代

Javascript JS Puppeter使用for循环在链接上迭代,javascript,node.js,puppeteer,Javascript,Node.js,Puppeteer,我正在尝试迭代独特的youtube视频链接以获得截图 调试之后,我注意到对于下面的forloop,JS生成了2个进程线程,每个进程线程对应一个索引I。第二个线程中的processALink()函数似乎在第一个线程中的processALink()完全结束之前启动 为什么会这样?我认为使用async/wait可以阻止这种情况的发生 forloop在异步函数中。下面的代码只是源代码的一个片段 for(let i = 0; i<2; i++){ var link = linksArr[i

我正在尝试迭代独特的youtube视频链接以获得截图

调试之后,我注意到对于下面的forloop,JS生成了2个进程线程,每个进程线程对应一个索引I。第二个线程中的processALink()函数似乎在第一个线程中的processALink()完全结束之前启动

为什么会这样?我认为使用async/wait可以阻止这种情况的发生

forloop在异步函数中。下面的代码只是源代码的一个片段

 for(let i = 0; i<2; i++){
    var link = linksArr[i];
    var label = labelsArr[i];
    await proccessALink(link, label)
  }
移除内部的
processALink()
,它应该可以解决同时运行多个屏幕截图的问题

const proccessALink = async(link, label) => {
  //set download path 
  const downloadPath = 'data/train/' + label;
  //parse the url
  const urlToScreenshot = parseUrl(link)
  //Give a URL it will take a screen shot 
  if (validUrl.isWebUri(urlToScreenshot)) {
    // console.log('Screenshotting: ' + urlToScreenshot + '&t=' + req.query.t)
    console.log('Screenshotting: ' + link);
    //Logic to login to youtube below
    //await login();
    //go to the url and wait till all the content is loaded.
    await page.goto(link, {
      waitUntil: 'networkidle'
      //waitUntil: 'domcontentloaded'
    })
    //await page.waitForNavigation();

    //Find the video player in the page 
    const video = await page.$('.html5-video-player')
    await page.content();

    //Run some command on consoleDev 
    await page.evaluate(() => {
      // Hide youtube player controls.
      let dom = document.querySelector('.ytp-chrome-bottom')
      if (dom != null) {
        dom.style.display = 'none'
      }
    })

    await video.screenshot({
      path: downloadPath
    });
  } else {
    res.send('Invalid url: ' + urlToScreenshot)
  }

}

请为
processALink()
@ryeballar添加函数定义,我已按要求添加了函数定义。我还注意到“page”变量由第二个线程共享。第二个线程在第一个线程结束之前更新页面变量。这是因为您在
processALink()
中定义了一个IIFE函数,而没有
wait
。此外,我认为您不需要这样定义。最后,如果您已经在使用
const
let
,那么您不需要使用
var
,因为它不尊重块范围。如果这已经解决了您的问题,请接受我在下面提供的答案。
const proccessALink = async(link, label) => {
  //set download path 
  const downloadPath = 'data/train/' + label;
  //parse the url
  const urlToScreenshot = parseUrl(link)
  //Give a URL it will take a screen shot 
  if (validUrl.isWebUri(urlToScreenshot)) {
    // console.log('Screenshotting: ' + urlToScreenshot + '&t=' + req.query.t)
    console.log('Screenshotting: ' + link);
    //Logic to login to youtube below
    //await login();
    //go to the url and wait till all the content is loaded.
    await page.goto(link, {
      waitUntil: 'networkidle'
      //waitUntil: 'domcontentloaded'
    })
    //await page.waitForNavigation();

    //Find the video player in the page 
    const video = await page.$('.html5-video-player')
    await page.content();

    //Run some command on consoleDev 
    await page.evaluate(() => {
      // Hide youtube player controls.
      let dom = document.querySelector('.ytp-chrome-bottom')
      if (dom != null) {
        dom.style.display = 'none'
      }
    })

    await video.screenshot({
      path: downloadPath
    });
  } else {
    res.send('Invalid url: ' + urlToScreenshot)
  }

}