保存cookies到本地

    1. const fs = require('fs').promises;
    2. // ... puppeteer code
    3. const cookies = await page.cookies();
    4. await fs.writeFile('./cookies.json', JSON.stringify(cookies, null, 2));

    从本地获取cookies

    1. const fs = require('fs').promises;
    2. // ... puppeteer code
    3. const cookiesString = await fs.readFile('./cookies.json');
    4. const cookies = JSON.parse(cookiesString);
    5. await page.setCookie(...cookies);