AttributeError: 'str' object has no attribute 'decode'

Can someone please help with this?

urls = search_images_ddg('grizzly bear', max_images=100)
len(urls),urls[0]s

---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-4-fe39ec9b75f1> in <module>
----> 1 urls = search_images_ddg('grizzly bear', max_images=100)
      2 len(urls),urls[0]

~\anaconda3\lib\site-packages\fastbook\__init__.py in search_images_ddg(term, max_images)
     55     assert max_images<1000
     56     url = 'https://duckduckgo.com/'
---> 57     res = urlread(url,data={'q':term}).decode()
     58     searchObj = re.search(r'vqd=([\d-]+)\&', res)
     59     assert searchObj

AttributeError: 'str' object has no attribute 'decode'
1 Like

Can you please prit what urlread returns?
print(urlread(url,data={‘q’:term})
Maybe it returned an error message (a string) that cannot be decoded?

1 Like

urlread now returns urls decoded as str per default, you can remove the .decode() from the function and it works.

2 Likes

Removed the .decode() recommended by @BresNet, fixed the issue. :slight_smile:

seems that the search_images_ddg() in (even the latest) fastbook still has the “.decode” in it.

urls = search_images_ddg('grizzly bear', max_images=100)
len(ims)

urls = search_images_ddg('grizzly bear', max_images=100)
len(ims)
urls = search_images_ddg('grizzly bear', max_images=100)
len(ims)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-12-e3064b7d2a3d> in <module>
----> 1 urls = search_images_ddg('grizzly bear', max_images=100)
      2 len(ims)

~/envs/fastai/lib/python3.8/site-packages/fastbook/__init__.py in search_images_ddg(term, max_images)
     55     assert max_images<1000
     56     url = 'https://duckduckgo.com/'
---> 57     res = urlread(url,data={'q':term}).decode()
     58     searchObj = re.search(r'vqd=([\d-]+)\&', res)
     59     assert searchObj

AttributeError: 'str' object has no attribute 'decode'