使用chrome-har导出浏览器HAR数据

这里使用nodejs下的chrome-har库来导出浏览器的har数据,经验证效果不错,比较靠谱。

const fs = require('fs');
const { promisify } = require('util');

const puppeteer = require('puppeteer');
const { harFromMessages } = require('chrome-har');

// 设置要监控的事件
const observe = [
  'Page.loadEventFired',
  'Page.domContentEventFired',
  'Page.frameStartedLoading',
  'Page.frameAttached',
  'Network.requestWillBeSent',
  'Network.requestServedFromCache',
  'Network.dataReceived',
  'Network.responseReceived',
  'Network.resourceChangedPriority',
  'Network.loadingFinished',
  'Network.loadingFailed',
];

/*
    启动浏览器
*/ 
async function launchBrowser(){
    //启动浏览器实例 [puppeteer.createBrowserFetcher([options])]
  let browser = await puppeteer.launch({
    // 若是手动下载的chromium需要指定chromium地址, 默认引用地址为 /项目目录/node_modules/puppeteer/.local-chromium/
    //executablePath: '/Users/huqiyang/Documents/project/z/chromium/Chromium.app/Contents/MacOS/Chromium',
    //如果是访问https页面 此属性会忽略https错误
    ignoreHTTPSErrors: true,
    // 关闭headless模式, 不会打开浏览器
    headless: false,
    //浏览器启动参数 https://peter.sh/experiments/chromium-command-line-switches/   --timeout
    args:["--disk-cache-size=0","--disable-cache",'--disable-infobars','--window-size=800,600','--ignore-certificate-errors','--enable-feaures'],
    //是否为每个选项卡自动打开DevTools面板。 如果此选项为true,则headless选项将设置为false。
    devtools: true,
    //Defaults to 30000 (30 seconds). Pass 0 to disable timeout.
    timeout: 0,
    //放慢puppeteer执行的动作,方便调试
    slowMo: 250
  });
  return browser ;
}

async function saveHarlog(url,filename){
  //打开浏览器
  let browser = await launchBrowser() ;

  //创建一个新页面
  //let page = await browser.newPage();
  let page = (await browser.pages())[0];

  // 注册事件监听器
  const client = await page.target().createCDPSession();
  await client.send('Page.enable');
  await client.send('Network.enable');

  //用于保存用于转为为HAR数据的事件
    const events = [];
  observe.forEach(method => {
    client.on(method, params => {
      events.push({ method, params });
    });
  });

  try{
    // 执行跳转,访问制定的资源
    await page.goto(url,{
            timeout:0
    });
  }catch(error){
    console.log('resovle error :' + url + ";  error message:" + error) ;
  }finally{
    if(browser){
        await browser.close();      
    }
  } 
  const har = harFromMessages(events);
  await promisify(fs.writeFile)(filename + '.har', JSON.stringify(har));

}
exports.launchBrowser = launchBrowser;
exports.saveHarlog = saveHarlog;

//测试
try{
    saveHarlog('http://www.baidu.com','www.baidu.com') ;    
}catch(error){}

参考地址:https://michaljanaszek.com/blog/generate-har-with-puppeteer

上一篇:javascript – 变量不分配内部承诺


下一篇:Nodejs中puppeteer抓取浏览器HAR数据