Chapter 2 DDG issues

Hello all,
I am trying to work my through chapter 2 using DDG instead of calling an API. It was going well up until I make a directory and try to put photos into the various folders. Please can someone advise where I am going wrong? Thank you.

1 Like

I used this code and it worked:

from duckduckgo_search import DDGS

def search_images(term, max_images=30):
    print(f"Searching for '{term}'")
    with DDGS() as ddgs:
        search_results = ddgs.images(keywords=term)
        image_urls = [next(search_results).get("image") for _ in range(max_images)]
        return L(image_urls)

bear_types = 'grizzly','black','teddy'
path = Path('bears')
from time import sleep
for o in bear_types:
       dest = (path/o)
       dest.mkdir(exist_ok=True, parents=True)
       download_images(dest, urls=search_images(f'{o} bear photo', max_images=100))
       sleep(10)
       resize_images(path/o, max_size=400, dest=path/o)
fns = get_image_files(path)
fns
3 Likes

Thank you, this worked a treat! Do you know why this worked and my attempt didn’t? What I wrote above was the same as the video

I’m not 100% sure (I’m a student as well). I think DuckDuckGo changed their API in the year since the course was filmed.