javascript - JS puppeteer 使用 for 循环迭代链接

标签 javascript node.js puppeteer

我正在尝试迭代独特的 YouTube 视频链接以获取屏幕截图。

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

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

for循环位于异步函数内。下面的代码只是原始源代码的一个片段。

 for(let i = 0; i<2; i++){
    var link = linksArr[i];
    var label = labelsArr[i];
    await proccessALink(link, label)
  }

processALink() 的函数定义

var proccessALink = async (link,label)=>{
    //set download path 
    var downloadPath  = 'data/train/'+label;
    //parse the url
    var 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)
      ;(async () => {

        //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)
    }

  }

最佳答案

删除 IIFEprocessALink() 内,它应该可以解决同时运行多个屏幕截图的问题。

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

}

关于javascript - JS puppeteer 使用 for 循环迭代链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59704843/

相关文章:

javascript - 将对象插入全局数组?

javascript - Javascript 全局命名空间中的保留标识符

javascript - NODE 中同时 GET 和 POST 请求

javascript - Node 中的永久 session 存储

javascript - 如何使用 Node puppeteer 获取页面中的所有链接?

javascript - 使用 AJAX 将 JSON 对象发送到服务器

javascript - 后台 HTML 监听器

node.js - 系统中security_token是什么意思, Node 堆快照中全局中native_context是什么意思

node.js - 为什么我不能使用 Puppeteer 在 exposeFunction() 函数中访问 'window'?

javascript - 如何使用Puppeteer库抓取动态网页返回值?