DEV Community

Big Man
Big Man

Posted on

How to get Google Images search results with an API (Python, Node or no code)

Google has no public API for Google Images search results. If you need images for a topic in bulk, with their full-size URLs, dimensions and source pages (for a mood board, a dataset, or to monitor where a product photo appears), saving them one by one from the browser doesn't scale.

This tutorial uses Google Images Scraper, an Apify actor I built. For each search it returns the full-size image URL, width and height, thumbnail, title, source page and website, at about 100 images per page. It has size, type and date filters, works for any country, and has an only-new mode for scheduled runs. The full examples are on GitHub: auto-actor/google-images-scraper.

What people use it for

  • Collecting reference or mood-board images for a topic, with source pages for attribution.
  • Monitoring where a product or brand image appears across the web, daily with onlyNew: true.
  • Building image datasets with dimensions to filter by size.

Input

{"queries": ["red sneakers"], "maxResultsPerQuery": 100, "country": "us"}
Enter fullscreen mode Exit fullscreen mode
Field What it does
queries Searches, written the way you would type them into Google.
maxResultsPerQuery The most images to return for each search (Google serves about 100 per page).
country Two-letter country code Google searches from, e.g. us, gb, de.
language Interface language code, e.g. en, de.
imageSize any, large, medium or icon.
imageType any, photo, clipart, lineart or animated.
timeRange any, day, week, month or year.
safeSearch Keep Google SafeSearch on.
onlyNew Skip images already returned by earlier runs (remembered for 90 days in the google-images-seen store of your account). Ideal for schedules.
debug Save raw HTML for debugging.

Python

"""Run Google Images Scraper on Apify and print the results.

    pip install apify-client
    export APIFY_TOKEN=...   # Apify Console -> Settings -> Integrations
    python example.py
"""
import os

from apify_client import ApifyClient

client = ApifyClient(os.environ["APIFY_TOKEN"])

run = client.actor("daedal_quintuplet/google-images-scraper").call(
    run_input={
        "queries": [
            "red sneakers"
        ],
        "maxResultsPerQuery": 100,
        "country": "us"
    }
)

images = client.dataset(run["defaultDatasetId"]).iterate_items()
for image in images:
    print(image["position"], image["title"], image["imageUrl"], f'{image["imageWidth"]}x{image["imageHeight"]}')
Enter fullscreen mode Exit fullscreen mode

Node.js

// Run Google Images Scraper on Apify and print the results.
//
//   npm install apify-client
//   APIFY_TOKEN=... node example.js     (token: Apify Console -> Settings -> Integrations)

const { ApifyClient } = require('apify-client');

async function main() {
    const client = new ApifyClient({ token: process.env.APIFY_TOKEN });

    const run = await client.actor('daedal_quintuplet/google-images-scraper').call({
        "queries": [
            "red sneakers"
        ],
        "maxResultsPerQuery": 100,
        "country": "us"
    });

    const { items } = await client.dataset(run.defaultDatasetId).listItems();
    for (const image of items) {
        console.log(image.position, image.title, image.imageUrl, `${image.imageWidth}x${image.imageHeight}`);
    }
}

main().catch((err) => {
    console.error(err);
    process.exit(1);
});
Enter fullscreen mode Exit fullscreen mode

No code: one HTTP request

Works from n8n, Make, Zapier or curl:

curl -X POST "https://api.apify.com/v2/acts/daedal_quintuplet~google-images-scraper/run-sync-get-dataset-items?token=$APIFY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"queries": ["red sneakers"], "maxResultsPerQuery": 100, "country": "us"}'
Enter fullscreen mode Exit fullscreen mode

What you get

One row per image:

Field Meaning
query The query that found it
position Rank in the results
imageId Google's image id
title Title Google shows
imageUrl Full-size image URL
imageWidth Full-size width in pixels
imageHeight Full-size height in pixels
thumbnailUrl Google's thumbnail
thumbnailWidth Thumbnail width
thumbnailHeight Thumbnail height
contentUrl The page the image appears on
origin Website domain
source Website name
fileSize File size when Google shows it
country Country searched from
scrapedAt When it was scraped

Notes

  • Images belong to their owners; check the source page's licence before reusing one.
  • Google serves about 100 images per page; use several specific queries for more.

Price

$0.0021 per image ($2.10 per 1,000), paid per result, with no monthly fee. A 100-image search costs about $0.21. Apify's free plan includes $5 of usage a month, which is enough to try it.

Links: Store page ยท GitHub examples

Top comments (0)