首页 > 解决方案 > 如何在javascript中迭代多个标头

问题描述

当我想在下载文件时只获取第一个“日期”标题时,我无法迭代选择第一个,5 个“日期”标题出来了。

Tue, 20 Aug 2019 17:40:36 GMT
Tue, 20 Aug 2019 17:40:37 GMT
Tue, 20 Aug 2019 17:40:37 GMT
Tue, 20 Aug 2019 17:40:37 GMT
Tue, 20 Aug 2019 17:40:37 GMT

我试过用headers.entries(),用for,foreach,都没有成功。

await Promise.all([

  await button.click(),
     page.on('response', response => {
          console.log(response._headers['date']);

      })
  ]);

标签: javascriptnode.jspuppeteer

解决方案


尝试这样的事情:

  var firstdate = null;
  await Promise.all([    
  await button.click(),
     page.on('response', response => {
          firstdate = firstdate == null ? response._headers['date'] : firstdate;
          console.log(response._headers['date']);

      })
  ]);

  console.log("first date", firstdate);

如果你只想在第一次采取行动,你可以做更多这样的事情

  var firstdate = true;
  await Promise.all([    
  await button.click(),
     page.on('response', response => {  
          firstdate && console.log(response._headers['date']); // can replace console.log with your action
          firstdate = false;

      })
  ]);

推荐阅读