ChartQuery

Collecter des images Google

Téléchargez et cataloguez les URLs d'images pour n'importe quelle requête de recherche avec l'API Autom — utile pour la constitution de datasets, la recherche visuelle ou les audits de contenus concurrents.

Vue d'ensemble

Ce guide montre comment collecter les URLs d'images, titres et dimensions depuis Google Images pour une requête donnée. Chaque résultat inclut l'URL directe de l'image, la page sur laquelle elle a été trouvée, son domaine source et ses dimensions en pixels.

Prérequis

  • Une clé API Autom — obtenez-en une sur app.autom.dev
  • Installez les dépendances pour votre langage :
pip install requests

Aucune dépendance supplémentaire — utilise l'API native fetch (Node 18+).

Extension curl activée (par défaut dans la plupart des installations PHP).

Aucune dépendance supplémentaire — utilise net/http (Go 1.18+).

Aucune dépendance supplémentaire — utilise java.net.http (Java 11+).

Aucune dépendance supplémentaire — utilise System.Net.Http (.NET 6+).

# Cargo.toml
[dependencies]
reqwest = { version = "0.12", features = ["json"] }
tokio = { version = "1", features = ["full"] }
serde_json = "1"

Étapes

Rechercher des images

Appelez GET /v1/google/images avec un paramètre q.

import requests

API_KEY = "YOUR_API_KEY"

response = requests.get(
    "https://api.autom.dev/v1/google/images",
    headers={"x-api-key": API_KEY},
    params={"q": "electric car charging station", "gl": "us", "hl": "en"},
)
data = response.json()
const API_KEY = "YOUR_API_KEY";

const params   = new URLSearchParams({ q: "electric car charging station", gl: "us", hl: "en" });
const response = await fetch(`https://api.autom.dev/v1/google/images?${params}`, {
  headers: { "x-api-key": API_KEY },
});
const data = await response.json();
<?php
$apiKey = "YOUR_API_KEY";
$params = http_build_query(["q" => "electric car charging station", "gl" => "us", "hl" => "en"]);

$ch = curl_init("https://api.autom.dev/v1/google/images?{$params}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["x-api-key: {$apiKey}"]);
$data = json_decode(curl_exec($ch), true);
curl_close($ch);
package main

import (
    "encoding/json"
    "io"
    "net/http"
    "net/url"
)

func main() {
    params := url.Values{"q": {"electric car charging station"}, "gl": {"us"}, "hl": {"en"}}
    req, _ := http.NewRequest("GET", "https://api.autom.dev/v1/google/images?"+params.Encode(), nil)
    req.Header.Set("x-api-key", "YOUR_API_KEY")

    resp, _ := http.DefaultClient.Do(req)
    defer resp.Body.Close()
    body, _ := io.ReadAll(resp.Body)

    var data map[string]any
    json.Unmarshal(body, &data)
}
import java.net.URI;
import java.net.http.*;

var client  = HttpClient.newHttpClient();
var request = HttpRequest.newBuilder()
    .uri(URI.create("https://api.autom.dev/v1/google/images?q=electric+car+charging+station&gl=us&hl=en"))
    .header("x-api-key", "YOUR_API_KEY")
    .GET().build();

var response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
using System.Net.Http;

using var client = new HttpClient();
client.DefaultRequestHeaders.Add("x-api-key", "YOUR_API_KEY");

var body = await client.GetStringAsync(
    "https://api.autom.dev/v1/google/images?q=electric+car+charging+station&gl=us&hl=en");
#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
    let data = reqwest::Client::new()
        .get("https://api.autom.dev/v1/google/images")
        .header("x-api-key", "YOUR_API_KEY")
        .query(&[("q", "electric car charging station"), ("gl", "us"), ("hl", "en")])
        .send().await?.json::<serde_json::Value>().await?;

    println!("{:#?}", data);
    Ok(())
}

Inspecter les résultats d'images

Chaque élément dans images possède url (image directe), link (page source), title, domain, source, image_width et image_height.

for img in data.get("images", []):
    print(f"[{img['position']}] {img['title']}")
    print(f"  Image : {img['url']}")
    print(f"  Source: {img['source']}{img['image_width']}x{img['image_height']}px\n")
for (const img of data.images ?? []) {
  console.log(`[${img.position}] ${img.title}`);
  console.log(`  Image : ${img.url}`);
  console.log(`  Source: ${img.source} — ${img.image_width}x${img.image_height}px\n`);
}
foreach ($data["images"] ?? [] as $img) {
    echo "[{$img['position']}] {$img['title']}\n";
    echo "  Image : {$img['url']}\n";
    echo "  Source: {$img['source']} — {$img['image_width']}x{$img['image_height']}px\n\n";
}
import "fmt"

for _, r := range data["images"].([]any) {
    img := r.(map[string]any)
    fmt.Printf("[%.0f] %s\n  Image : %s\n  Source: %s%.0fx%.0fpx\n\n",
        img["position"], img["title"], img["url"],
        img["source"], img["image_width"], img["image_height"])
}
import org.json.*;

var images = new JSONObject(response.body()).getJSONArray("images");
for (int i = 0; i < images.length(); i++) {
    var img = images.getJSONObject(i);
    System.out.printf("[%d] %s%n  Image : %s%n  Source: %s — %dx%dpx%n%n",
        img.getInt("position"), img.getString("title"),
        img.getString("url"), img.getString("source"),
        img.getInt("image_width"), img.getInt("image_height"));
}
using System.Text.Json;

var images = JsonDocument.Parse(body).RootElement.GetProperty("images").EnumerateArray();
foreach (var img in images)
{
    Console.WriteLine($"[{img.GetProperty("position")}] {img.GetProperty("title")}");
    Console.WriteLine($"  Image : {img.GetProperty("url")}");
    Console.WriteLine($"  Source: {img.GetProperty("source")}{img.GetProperty("image_width")}x{img.GetProperty("image_height")}px\n");
}
if let Some(images) = data["images"].as_array() {
    for img in images {
        println!("[{}] {}", img["position"], img["title"].as_str().unwrap_or(""));
        println!("  Image : {}", img["url"].as_str().unwrap_or(""));
        println!("  Source: {} — {}x{}px\n",
            img["source"].as_str().unwrap_or(""), img["image_width"], img["image_height"]);
    }
}

Construire un catalogue d'images filtré par taille minimale

Filtrez les miniatures et ne conservez que les images haute résolution.

import csv, requests

API_KEY   = "YOUR_API_KEY"
QUERY     = "electric car charging station"
MIN_WIDTH = 800

def fetch_images(query: str, pages: int = 2) -> list:
    results = []
    for page in range(1, pages + 1):
        r = requests.get("https://api.autom.dev/v1/google/images",
            headers={"x-api-key": API_KEY},
            params={"q": query, "gl": "us", "hl": "en", "page": page})
        results.extend(r.json().get("images", []))
    return results

large = [img for img in fetch_images(QUERY) if img.get("image_width", 0) >= MIN_WIDTH]

with open("image_catalog.csv", "w", newline="") as f:
    writer = csv.DictWriter(f, fieldnames=["position", "title", "url", "source", "image_width", "image_height"])
    writer.writeheader()
    writer.writerows(large)

print(f"Sauvegardé {len(large)} images haute résolution dans image_catalog.csv")
import { writeFileSync } from "fs";

const API_KEY   = "YOUR_API_KEY";
const MIN_WIDTH = 800;

async function fetchImages(query: string, pages = 2): Promise<any[]> {
  const all: any[] = [];
  for (let page = 1; page <= pages; page++) {
    const params = new URLSearchParams({ q: query, gl: "us", hl: "en", page: String(page) });
    const res    = await fetch(`https://api.autom.dev/v1/google/images?${params}`, {
      headers: { "x-api-key": API_KEY },
    });
    all.push(...((await res.json()).images ?? []));
  }
  return all;
}

const images = await fetchImages("electric car charging station");
const large  = images.filter(img => (img.image_width ?? 0) >= MIN_WIDTH);
writeFileSync("image_catalog.json", JSON.stringify(large, null, 2));
console.log(`Sauvegardé ${large.length} images haute résolution dans image_catalog.json`);
<?php
$apiKey   = "YOUR_API_KEY";
$query    = "electric car charging station";
$minWidth = 800;

function fetchImages(string $apiKey, string $query, int $pages = 2): array {
    $all = [];
    for ($page = 1; $page <= $pages; $page++) {
        $params = http_build_query(["q" => $query, "gl" => "us", "hl" => "en", "page" => $page]);
        $ch = curl_init("https://api.autom.dev/v1/google/images?{$params}");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ["x-api-key: {$apiKey}"]);
        $data = json_decode(curl_exec($ch), true);
        curl_close($ch);
        $all = array_merge($all, $data["images"] ?? []);
    }
    return $all;
}

$large = array_filter(fetchImages($apiKey, $query), fn($img) => ($img["image_width"] ?? 0) >= $minWidth);
file_put_contents("image_catalog.json", json_encode(array_values($large), JSON_PRETTY_PRINT));
echo "Sauvegardé " . count($large) . " images haute résolution dans image_catalog.json\n";
package main

import (
    "encoding/json"
    "fmt"
    "io"
    "net/http"
    "net/url"
    "os"
    "strconv"
)

func fetchImages(apiKey, query string, pages int) []map[string]any {
    var all []map[string]any
    for page := 1; page <= pages; page++ {
        params := url.Values{"q": {query}, "gl": {"us"}, "hl": {"en"}, "page": {strconv.Itoa(page)}}
        req, _ := http.NewRequest("GET", "https://api.autom.dev/v1/google/images?"+params.Encode(), nil)
        req.Header.Set("x-api-key", apiKey)
        resp, _ := http.DefaultClient.Do(req)
        body, _ := io.ReadAll(resp.Body)
        resp.Body.Close()

        var data map[string]any
        json.Unmarshal(body, &data)
        for _, r := range data["images"].([]any) {
            all = append(all, r.(map[string]any))
        }
    }
    return all
}

func main() {
    images    := fetchImages("YOUR_API_KEY", "electric car charging station", 2)
    minWidth  := float64(800)
    var large []map[string]any
    for _, img := range images {
        if img["image_width"].(float64) >= minWidth {
            large = append(large, img)
        }
    }
    b, _ := json.MarshalIndent(large, "", "  ")
    os.WriteFile("image_catalog.json", b, 0644)
    fmt.Printf("Sauvegardé %d images haute résolution dans image_catalog.json\n", len(large))
}
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.*;
import org.json.*;

public class Main {
    public static void main(String[] args) throws Exception {
        var apiKey   = "YOUR_API_KEY";
        var query    = URLEncoder.encode("electric car charging station", StandardCharsets.UTF_8);
        var minWidth = 800;
        var client   = HttpClient.newHttpClient();
        var all      = new JSONArray();

        for (int page = 1; page <= 2; page++) {
            var url  = "https://api.autom.dev/v1/google/images?q=" + query + "&gl=us&hl=en&page=" + page;
            var req  = HttpRequest.newBuilder().uri(URI.create(url))
                .header("x-api-key", apiKey).GET().build();
            var resp = client.send(req, HttpResponse.BodyHandlers.ofString());
            var imgs = new JSONObject(resp.body()).getJSONArray("images");
            for (int i = 0; i < imgs.length(); i++) all.put(imgs.get(i));
        }

        var large = new JSONArray();
        for (int i = 0; i < all.length(); i++) {
            var img = all.getJSONObject(i);
            if (img.getInt("image_width") >= minWidth) large.put(img);
        }
        Files.writeString(Path.of("image_catalog.json"), large.toString(2));
        System.out.println("Sauvegardé " + large.length() + " images haute résolution dans image_catalog.json");
    }
}
using System.Net.Http;
using System.Text.Json;

var apiKey   = "YOUR_API_KEY";
var minWidth = 800;
using var client = new HttpClient();
client.DefaultRequestHeaders.Add("x-api-key", apiKey);

var all = new List<JsonElement>();
for (int page = 1; page <= 2; page++)
{
    var body = await client.GetStringAsync(
        $"https://api.autom.dev/v1/google/images?q=electric+car+charging+station&gl=us&hl=en&page={page}");
    var json = JsonDocument.Parse(body).RootElement;
    foreach (var img in json.GetProperty("images").EnumerateArray())
        all.Add(img);
}

var large = all.Where(img => img.GetProperty("image_width").GetInt32() >= minWidth).ToList();
File.WriteAllText("image_catalog.json", JsonSerializer.Serialize(large, new JsonSerializerOptions { WriteIndented = true }));
Console.WriteLine($"Sauvegardé {large.Count} images haute résolution dans image_catalog.json");
use reqwest::Client;
use serde_json::Value;
use std::fs;

async fn fetch_images(client: &Client, api_key: &str, query: &str, pages: u32) -> Vec<Value> {
    let mut all = Vec::new();
    for page in 1..=pages {
        let data = client.get("https://api.autom.dev/v1/google/images")
            .header("x-api-key", api_key)
            .query(&[("q", query), ("gl", "us"), ("hl", "en"), ("page", &page.to_string())])
            .send().await.unwrap().json::<Value>().await.unwrap();
        if let Some(imgs) = data["images"].as_array() { all.extend(imgs.clone()); }
    }
    all
}

#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
    let client = Client::new();
    let images = fetch_images(&client, "YOUR_API_KEY", "electric car charging station", 2).await;
    let large: Vec<&Value> = images.iter()
        .filter(|img| img["image_width"].as_i64().unwrap_or(0) >= 800)
        .collect();
    fs::write("image_catalog.json", serde_json::to_string_pretty(&large).unwrap()).unwrap();
    println!("Sauvegardé {} images haute résolution dans image_catalog.json", large.len());
    Ok(())
}

Le champ url dans chaque résultat est le lien direct vers le fichier image. Vérifiez toujours que vous disposez des droits d'utilisation d'une image avant de l'inclure dans un dataset ou un produit.

Sur cette page