Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The process for downloading images with authentication query strings in the image URL when using Puppeteer as a tool can be achieved through the following steps:

  1. First, launch a headless Chrome browser using Puppeteer:
const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  // your code here
  await browser.close();
})();
  1. Navigate to the website that contains the images you want to download:
await page.goto('https://example.com');
  1. Find the URLs of the images you want to download. For example, if the image is located in an <img> tag with a src attribute, you can use the following code to extract the URL:
const imageURL = await page.$eval('img', (img) => img.src);
  1. Modify the URL to include the authentication query string. For example, if the authentication string is ?token=123456789, you would add it to the URL using the following code:
const authenticatedURL = imageURL + '?token=123456789';
  1. Use the request library to download the image:
const request = require('request');
const fs = require('fs');

request(authenticatedURL)
    .pipe(fs.createWriteStream('image.jpg'));

This code downloads the image and saves it as image.jpg in the current directory.