Search code examples
javascriptweb-scrapingscreen-scrapingchromiumpuppeteer

How to write data to a file using Puppeteer?


Puppeteer exposes a page.screenshot() method for saving a screenshot locally on your machine. Here are the docs.

See: https://github.com/GoogleChrome/puppeteer
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await page.screenshot({path: 'example.png'});

Is there a way to save a data file in a similar fashion. I'm seeking something analogous to...

page.writeToFile({data, path,});

Solution

  • Since any puppeteer script is an ordinary node.js script you can use anything you would use in node, say the good old fs module:

    const fs = require('fs');
    fs.writeFileSync('path/to/file.json', data);