Ddg_images doesnt return any results

Hello,
I am currently reviewing the first part of the course because it has been a couple of weeks since I last looked at it and am stuck at searching urls with the ddg_images function.

The method used in the video course didnt work for me, so I used the method presented in the according kaggle notebook. The last time I used it it worked, this time it didnt and I dont know why.

from duckduckgo_search import ddg_images
from fastcore.all import *

def search_images(term, max_images=30):
    print(f"Searching for '{term}'")
    return L(ddg_images(term, max_results=max_images)).itemgot('image')

This function will return a empty list no matter the input term, but it wont throw any exceptions.

Does anybody know what the problem could be?

if you want the function on the video to work import this first

import fastbook
from fastbook import *

and then search_images_ddg will work :slight_smile:

3 Likes

Yes it worked, dont know how that slipped past me when I tried it :sweat_smile:
Thank you!

1 Like

I’m having the same problem, did the recommended fix and still am not getting any results. Using Python3.9 on Mac Intel

Guys, I think its not an issue with any import or library. It seems like ddg is blocking the ip address. So when you restart the router/ change public ip address it will work for once.

1 Like

I did have the same experience as you Dev Patel when using ddg_images, restarted notebook and it worked once.

I am now using search_images_ddg per Kamui’s suggestion and it works for me.

This works for me:

This does not:

1 Like

Yup, its working thanks. :smile:

I cannot make this work, can anyone share a working notebook?

I used what (Profile - hazarath - fast.ai Course Forums) suggested in another thread and it worked :slight_smile:
"yeah, ddg_images is depricated
you can replace

from duckduckgo_search import ddg_images
def search_images(term, max_images=30):
    print(f"Searching for '{term}'")
    return L(ddg_images(term, max_results=max_images)).itemgot('image')

with

from duckduckgo_search import DDGS

def search_images(keywords, max_images = 30):
    print(f"Searching for {keywords}")
    return L(DDGS().images(keywords,max_results=max_images)).itemgot('image')

Hello all, I am having the same issue. I have updated the code but it keeps blowing up, would someone be able to help:
from fastcore.all import *

from duckduckgo_search import DDGS

def search_images(keywords, max_images = 30):
print(f"Searching for {keywords}")
return L(DDGS().images(keywords,max_results=max_images)).itemgot(‘image’)

urls = search_images(‘bird photos’, max_images=1)
urls[0]

Which returns a rather long error, but ultimately ends up with:
TypeError: images() got an unexpected keyword argument ‘max_results’

Any help would be appreciated!
Thank you

@cdsmith007 I updated the original nb to use duckduckgo_search in asyn mode, see [bird-notebook]:

%pip install --upgrade --quiet fastai fastcore fastdownload duckduckgo-search

from duckduckgo_search import AsyncDDGS
import asyncio

async def search_images(keywords, max_results=5):
    async with AsyncDDGS() as ddgs:
        results = [r['image'] async for r in ddgs.images(keywords, max_results=max_results)]
        return results

# search/download 'bird' image
dest = 'temp.jpg'

r = await search_images("bird")
download_url(r[0], dest=dest, show_progress=False)
im = Image.open(dest)
im.to_thumb(256,256)

this should work with duckduckgo-search==4.4.3

1 Like

Thank you, I will try this.
I actually just got it to work. For some strange reason it wouldn’t work in Kaggle but would work in google colab. Not sure why but relieved!