Duckduckgo search not working

But how do i do it on colab?

Here is the code that works in the colab

pip install -U duckduckgo_search

from duckduckgo_search import DDGS
from fastcore.all import *
from fastai.vision.all import *
from fastdownload import download_url
import random

def search_images(term, max_images=30):
    print(f"Searching for '{term}'")
    with DDGS() as ddgs:
        # generator which yields dicts with:
        # {'title','image','thumbnail','url','height','width','source'}
        search_results = ddgs.images(keywords=term)       
        # grap number of max_images urls
        image_urls = [result.get("image") for result in search_results[:max_images]] 
        # convert to L (functionally extended list class from fastai)
        return L(image_urls)

urls = search_images("bird images", max_images=10)
print(urls[0])

download_url(search_images('big bird photos', max_images=1)[0], '../images/bird.jpg', show_progress=False)
Image.open('../images/bird.jpg').to_thumb(256,256)
1 Like

Just thought I would add to this.

There is a function in fastbook called search_images_ddg (as mentioned by @maitland) that will fetch images.
The code:

import fastbook
from fastbook import *

def search_images(term, max_images=30):
    print(f"Searching for '{term}'")
    return L(search_images_ddg(term, max_images))

and then

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

works for me

Thanks Sergeii,
Can you confirm why a with statement is used here. I understand it instantiates a DDGS object for use in the following lines but the following works so what is wrong with this approach please?

def search_images(term, max_images=30):
    print(f"Searching for '{term}'")  
    search_results = DDGS(headers = {'Accept-Encoding': 'gzip, deflate, br'}).images(keywords=term)
    image_data = list(search_results)
    image_urls = [item.get("image") for item in search_results[:max_images]]
    return L(image_urls)

Thank you - fixed it for me

A question please: Why is the ‘with’ statement used to instantiate the DDGS object? Why cant we just use:

def search_images(term, max_images=30):
    print(f"Searching for '{term}'")  
    search_results = DDGS(headers = {'Accept-Encoding': 'gzip, deflate, br'}).images(keywords=term)
    image_data = list(search_results)
    image_urls = [item.get("image") for item in search_results[:max_images]]
    return L(image_urls)

Glad it helped! Regarding your question, I don’t know why that is needed as I’m not so familiar with that library’s methods.

Thanks for the quick reply @vbakshi

(Am guessing that there is concern that the search does not close resources completely after it has run.)

1 Like

@hazarath This works like a charm! Thank you so much!

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')
1 Like

Thanks @rmclynch It worked for me , i did lot of try but your solution worked

thanks @periculo
thanks @rmclynch for your confirmation.

This code worked for me today on the kaggle notebook

In kaggle I just used code cell

pip install -U duckduckgo_search

you should change string ‘bird photos’ → ‘bird’