Problem downloading images in Chapter 2 ("Downloading images with DuckDuckGo")

Hi everyone :waving_hand:

I’m working through Chapter 2 of the fastai book, where we use DuckDuckGo to download images for the bear classifier example.

I’m using the latest version of the duckduckgo-search library (now imported as from ddgs import DDGS), but I keep getting this error when I run the search_images_ddg function:

TypeError: images() missing 1 required positional argument: 'query'

Even after updating the function according to some GitHub issues, I still get 0 results or multiple HTTP 403 errors when trying to download the URLs returned by DDGS.
Here’s a simplified version of my code:

from ddgs import DDGS
from fastai.vision.all import *
import time

def search_images_ddg(term, max_results=150):
    all_urls = []
    with DDGS() as ddgs:
        results = ddgs.images(
            term,
            region='wt-wt',
            safesearch='moderate'
        )
        all_urls.extend([r['image'] for r in results if 'image' in r])
    return list(set(all_urls))[:max_results]

bear_types = ['grizzly', 'black', 'teddy']
path = Path('bears')
path.mkdir(exist_ok=True)

for o in bear_types:
    dest = path / o
    dest.mkdir(exist_ok=True)
    urls = search_images_ddg(f'{o} bear', max_results=150)
    print(f"{o}: {len(urls)} images found")

I tried several variations (e.g. passing the term as query=term, keywords=term, etc.) and verified that the DDGS version is recent (duckduckgo-search==8.1.1).
However, the API seems to have changed and I’m not sure what’s the correct usage for the current version — or whether there’s a better alternative (Unsplash, Pexels, etc.) that others are using now for this lesson.

Has anyone managed to get this example working recently?
Any tips on how to download image datasets safely without triggering 403 or Cloudflare blocks would be greatly appreciated :folded_hands:

Thanks in advance!

1 Like

What environment are you running your code in? When I opened a blank Colab notebook and tried to run the code you provided it seemed to run without hitting the type error you are getting(see attached screenshot).


Maybe try reinstalling ddgs to make sure you have the latest version.

pip uninstall -y duckduckgo-search ddgs
pip install ddgs

1 Like

I was having problems with this, but have got it working now. I am using ddgs v9.8.0.

from ddgs import DDGS

def search_images(term, max_results=150):
    results = DDGS().images(
        query=term,
        max_results=max_results,
    )
    return L([result['image'] for result in results])

if not path.exists():    path.mkdir()
for o in bear_types:
    dest = (path/o)
    dest.mkdir(exist_ok=True)
    results = search_images(f'{o} bear')
    download_images(dest, urls=results)

This worked for me. Hope that helps!

I had trouble using duckduckgo so I wrote a quick script to download the images with the Brave Search API. You do need a key but it is free.

import time
from pathlib import Path
from fastai.vision.all import (
    download_images,
    verify_images,
)
import requests

API_KEY = "API_KEY_HERE"
IMAGES_PER_BEAR = 150

bears = ["grizzly", "black", "teddy"]

path = Path("bears")
path.mkdir(exist_ok=True)

for bear in bears:
    dest = path / bear
    dest.mkdir(exist_ok=True)
    print(f"Downloading images for '{bear}' bear...")
    res = requests.get(
        "https://api.search.brave.com/res/v1/images/search",
        headers={
            "Accept": "application/json",
            "Accept-Encoding": "gzip",
            "x-subscription-token": API_KEY,
        },
        params={"q": f"{bear} bear", "count": f"{IMAGES_PER_BEAR}"},
    ).json()
    time.sleep(5) # Hit API rate limit without this
    
    urls = [result['properties']['url'] for result in res['results']]
    download_images(dest, urls=urls)

My code was working before. Now I just get this for search_image:

ModuleNotFoundError: No module named ‘fake_useragent’
I won’t share the codeblock because I’ve tried all the ones mentioned in this thread and continue to get the same error whether i’m in Kaggle or Colab.

Is there a definitive 2025 -Lesson 2 notebook for the video? It would be most helpful as things has changed since 2022.
When I use colab search_images_ddg does not work, nor does search_images (which i believe has search_images_ddg in the method definition). What is the single source of truth for this lesson as of November 2025? Thank you for any help or guidance you can give.

UPDATE: For whatever reason, I ran this in Kaggle even though my original version of ddgs was the updated to 9.8.1. It worked. Random magic. :man_shrugging:

!pip uninstall -y duckduckgo-search ddgs
!pip install ddgs

From this thread: Problem downloading images in Chapter 2 ("Downloading images with DuckDuckGo") - #2 by laptops