Hi everyone ![]()
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 ![]()
Thanks in advance!
