现在在kaggle上学习一个课程,第一课主要是识别图片里面是不是鸟🐦。其中一步是使用duckduckgo 搜索图片,源码:
from duckduckgo_search import ddg_images
from fastcore.all import *
from fastbook import search_images_ddg
def search_images(term, max_images=30):
print(f"Searching for '{term}'")
return L(search_images_ddg(term, max_results=max_images)).itemgot('image')
#NB: `search_images` depends on duckduckgo.com, which doesn't always return correct responses.
# If you get a JSON error, just try running it again (it may take a couple of tries).
urls = search_images('bird photos', max_images=1)
urls[0]
直接出现了下面的错误
opt/conda/lib/python3.7/site-packages/duckduckgo_search/compat.py:60: UserWarning: ddg_images is deprecated. Use DDGS().images() generator
warnings.warn("ddg_images is deprecated. Use DDGS().images() generator")
/opt/conda/lib/python3.7/site-packages/duckduckgo_search/compat.py:64: UserWarning: parameter page is deprecated
warnings.warn("parameter page is deprecated")
/opt/conda/lib/python3.7/site-packages/duckduckgo_search/compat.py:66: UserWarning: parameter max_results is deprecated
warnings.warn("parameter max_results is deprecated")
---------------------------------------------------------------------------
HTTPStatusError Traceback (most recent call last)
/tmp/ipykernel_18/2432147335.py in <module>
1 #NB: `search_images` depends on duckduckgo.com, which doesn't always return correct responses.
2 # If you get a JSON error, just try running it again (it may take a couple of tries).
----> 3 urls = search_images('bird photos', max_images=1)
4 urls[0]
/tmp/ipykernel_18/1717929076.py in search_images(term, max_images)
4 def search_images(term, max_images=30):
5 print(f"Searching for '{term}'")
----> 6 return L(ddg_images(term, max_results=max_images)).itemgot('image')
/opt/conda/lib/python3.7/site-packages/duckduckgo_search/compat.py in ddg_images(keywords, region, safesearch, time, size, color, type_image, layout, license_image, max_results, page, output, download)
80 type_image=type_image,
81 layout=layout,
---> 82 license_image=license_image,
83 ):
84 results.append(r)
/opt/conda/lib/python3.7/site-packages/duckduckgo_search/duckduckgo_search.py in images(self, keywords, region, safesearch, timelimit, size, color, type_image, layout, license_image)
425 cache = set()
426 for _ in range(10):
--> 427 resp = self._get_url("GET", "https://duckduckgo.com/i.js", params=payload)
428 if resp is None:
429 break
/opt/conda/lib/python3.7/site-packages/duckduckgo_search/duckduckgo_search.py in _get_url(self, method, url, **kwargs)
87 logger.warning(f"_get_url() {url} {type(ex).__name__} {ex}")
88 if i >= 2 or "418" in str(ex):
---> 89 raise ex
90 sleep(3)
91 return None
/opt/conda/lib/python3.7/site-packages/duckduckgo_search/duckduckgo_search.py in _get_url(self, method, url, **kwargs)
81 if self._is_500_in_url(str(resp.url)) or resp.status_code == 202:
82 raise httpx._exceptions.HTTPError("")
---> 83 resp.raise_for_status()
84 if resp.status_code == 200:
85 return resp
/opt/conda/lib/python3.7/site-packages/httpx/_models.py in raise_for_status(self)
747 error_type = error_types.get(status_class, "Invalid status code")
748 message = message.format(self, error_type=error_type)
--> 749 raise HTTPStatusError(message, request=request, response=self)
750
751 def json(self, **kwargs: typing.Any) -> typing.Any:
HTTPStatusError: Client error '403 Forbidden' for url 'https://duckduckgo.com/i.js?l=wt-wt&o=json&s=0&q=bird%20photos&vqd=4-120319640991756807142594628538299407667&f=%2C%2C%2C%2C%2C&p=1'
For more information check: https://httpstatuses.com/403
另外我还尝试了在Google的colab里面试了一下,也出现了下面的错误
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-10-dd751e9bd5d7> in <cell line: 1>()
----> 1 from duckduckgo_search import ddg_images
2 from fastcore.all import *
3
4 def search_images(term, max_images=30):
5 print(f"Searching for '{term}'")
ImportError: cannot import name 'ddg_images' from 'duckduckgo_search' (/usr/local/lib/python3.10/dist-packages/duckduckgo_search/__init__.py)
其实还是版本的问题,做下面的修复即可:
原来的search_images_ddg API已经不能用了,直接用fastbook中新的API search_images_ddg 即可,并且新的API似乎不支持。
!pip install fastbook
from fastbook import search_images_ddg
# urls = search_images('bird photos', max_images=1)
urls = search_images_ddg('bird photos', max_images=1)
urls[0]
好了,大功告成了,还有问题的话可以参考一下我的note: