Website Image Downloader logoWebsite Image Downloader

Home › Guides

Extract and download images from a website with JavaScript (Node.js)

DIY: fetch + cheerio

import * as cheerio from 'cheerio'; // npm i cheerio

const largest = (srcset = '') => srcset.split(/,\s+/).map((p) => p.trim().split(/\s+/))
    .filter(([u]) => u && !/^(data|blob):/.test(u))
    .map(([u, d = '1x']) => [u, (parseFloat(d) || 1) * (d.endsWith('w') ? 1 : 1000)])
    .sort((a, b) => b[1] - a[1])[0]?.[0];

export async function imageUrls(pageUrl) {
    const html = await (await fetch(pageUrl, { headers: { 'user-agent': 'Mozilla/5.0' } })).text();
    const $ = cheerio.load(html);
    const out = new Set();
    $('img').each((_, el) => {
        const $el = $(el);
        const src = largest($el.attr('srcset')) || largest($el.attr('data-srcset'))
            || $el.attr('data-src') || $el.attr('data-lazy-src') || $el.attr('src');
        if (src && !src.startsWith('data:')) out.add(new URL(src, pageUrl).href);
    });
    $('picture source[srcset]').each((_, el) => {
        const best = largest($(el).attr('srcset'));
        if (best) out.add(new URL(best, pageUrl).href);
    });
    const og = $('meta[property="og:image"]').attr('content');
    if (og) out.add(new URL(og, pageUrl).href);
    return [...out];
}

console.log(await imageUrls('https://www.example.com/'));

Download with fetch, read dimensions with image-size, hash with crypto.createHash('sha256').

Hosted: apify-client

import { ApifyClient } from 'apify-client'; // npm i apify-client

const client = new ApifyClient({ token: process.env.APIFY_TOKEN });
const run = await client.actor('sste/website-image-downloader').call({
    "startUrls": [
        {
            "url": "https://www.example.com/"
        }
    ],
    "minWidth": 300,
    "minHeight": 300
});
const { items } = await client.dataset(run.defaultDatasetId).listItems();
console.log(items.map((i) => `${i.width}x${i.height} ${i.imageUrl}`));

More: examples on GitHub, API reference.

Related

Try it on Apify → API docs Use with AI agents (MCP)