Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Scheduled Screenshots of Public Web Pages: How-To

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Automating screenshots of public web pages lets you track changes over time, monitor competitor pricing, archive news articles, or keep a visual record for compliance. This guide walks you through setting up a free, reliable system using a headless browser and a scheduler, so you can capture snapshots at set intervals without manual effort.

What You'll Need

  • A computer that stays on during your scheduled times (a small VPS, a Raspberry Pi, or your main desktop).
  • Node.js (version 14 or later) installed. Download from nodejs.org.
  • Basic comfort with the command line (Terminal on macOS/Linux, Command Prompt or PowerShell on Windows).
  • Optional: a cloud storage account (like Dropbox or Google Drive) if you want the screenshots accessible from anywhere.

Step-by-Step

  1. Set up a project folder. Open your terminal and run: mkdir web-screenshots && cd web-screenshots
  2. Initialize the project. Run npm init -y to create a package.json file.
  3. Install Puppeteer. Puppeteer is a Node.js library that controls headless Chrome. Run: npm install puppeteer This will download a compatible version of Chrome automatically.
  4. Create the screenshot script. Create a file named screenshot.js with the following content:
    const puppeteer = require('puppeteer');
    const fs = require('fs');
    
    const urls = [
      'https://example.com',
      'https://example.org'
    ];
    
    (async () => {
      const browser = await puppeteer.launch();
      const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
      fs.mkdirSync('shots', { recursive: true });
    
      for (const url of urls) {
        const page = await browser.newPage();
        await page.goto(url, { waitUntil: 'networkidle2', timeout: 60000 });
        await page.screenshot({ path: `shots/${url.replace(/^https?:\/\//, '').replace(/\//g, '_')}_${timestamp}.png`, fullPage: true });
        await page.close();
      }
    
      await browser.close();
      console.log('Screenshots saved.');
    })();
        
    Replace the URLs with the ones you want to capture. The script saves full-page screenshots with a timestamp in the filename.
  5. Test the script. Run node screenshot.js in your terminal. Check the shots folder for the PNG files.
  6. Schedule the script. Now you'll set up a recurring task.
    • On macOS/Linux (cron): Open your crontab with crontab -e and add a line like this to run daily at 9 AM:
      0 9 * * * cd /path/to/web-screenshots && /usr/bin/node screenshot.js
      Adjust the path to your project folder and the node binary (find it with which node).
    • On Windows (Task Scheduler): Open Task Scheduler, create a new task, set the trigger to your desired time, and the action to run node.exe with the argument screenshot.js and the working directory set to your project folder.
  7. Optional: Upload to cloud storage. To sync the shots folder, point your cloud client to it, or add a line in the script to upload via an API (e.g., using the Dropbox API).

Common Mistakes

  • Using the wrong node path in cron. Cron has a limited PATH. Always use the absolute path to node (find it with which node).
  • Not using networkidle2. If you use the default load event, the page may not have finished loading dynamic content, resulting in incomplete screenshots.
  • Forgetting that the computer must be on. If your machine is off during the scheduled time, the screenshot won't be taken. Use a VPS or a machine that's always on.
  • Overwriting files. If you don't include a timestamp in the filename, each run will overwrite the previous screenshot. The script above includes a timestamp.
  • Ignoring memory usage. Puppeteer uses significant memory. If you have many URLs, consider processing them sequentially (as in the script) or using a queue.

Tips

  • Set a reasonable timeout. Some pages take longer to load. Increase the timeout in page.goto() if you get timeouts.
  • Use fullPage: true to capture the entire page, not just the viewport. This is often what you want for archiving.
  • Handle login-protected pages. If you need to capture pages behind a login, use Puppeteer to fill in credentials first, but be aware that this may violate the site's terms of service.
  • Compress old screenshots. Over time, the folder will grow. Use a cron job to zip files older than 30 days.
  • Run the script manually after changing it. Always test after editing to avoid silent failures in cron.

Troubleshooting

  • No screenshots appear. Check the cron logs (usually in /var/log/syslog or /var/log/cron). Run the script manually to see any error output.
  • Puppeteer fails to launch Chrome. This often happens on Linux servers missing dependencies. Run npx puppeteer browsers install chrome or install the required libraries (e.g., libnss3, libatk).
  • Screenshots are blank or partial. The page may not have loaded fully. Increase the timeout, or use waitUntil: 'networkidle0' for stricter conditions.
  • Script works manually but not from cron. This is almost always an environment issue. Ensure cron uses the correct node path and that the working directory is set.
  • Memory errors with many URLs. Close each page after capturing (as in the script) and consider using --no-sandbox if running as root (not recommended for security).

Frequently Asked Questions

Can I capture screenshots of pages that require JavaScript?

Yes, Puppeteer runs a full Chromium browser, so JavaScript-heavy pages render just like in a normal browser.

Is it legal to screenshot any public web page?

Generally, yes, but you should respect the site's robots.txt and terms of service. For personal archiving, this is typically fine. For commercial use, check the site's policies.

How often can I run the script?

As often as you like, but be considerate of the server's resources. Running every minute might get your IP blocked. Daily or hourly is usually safe for most sites.

Can I capture only a specific element instead of the full page?

Yes, use page.$() to select an element and then element.screenshot() to capture just that element.

What if the page takes longer than 60 seconds to load?

Increase the timeout value in page.goto() to, say, 120000 (2 minutes).

Can I run this on a free cloud service?

Some free tiers (like GitHub Actions or Heroku free dynos) allow scheduled jobs, but they may have limitations on runtime or require a credit card. A low-cost VPS is more reliable for continuous scheduling.