找第三方做网站 需要注意,临夏州建设网站,廊坊企业网站服务,濮阳做网站的现在在kaggle上学习一个课程#xff0c;第一课主要是识别图片里面是不是鸟#x1f426;。其中一步是使用duckduckgo 搜索图片#xff0c;源码#xff1a;
from duckduckgo_search import ddg_images
from fastcore.all import *
from fastbook import search_images_ddgde…现在在kaggle上学习一个课程第一课主要是识别图片里面是不是鸟。其中一步是使用duckduckgo 搜索图片源码
from duckduckgo_search import ddg_images
from fastcore.all import *
from fastbook import search_images_ddgdef search_images(term, max_images30):print(fSearching for {term})return L(search_images_ddg(term, max_resultsmax_images)).itemgot(image)
#NB: search_images depends on duckduckgo.com, which doesnt 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_images1)
urls[0]
直接出现了下面的错误
opt/conda/lib/python3.7/site-packages/duckduckgo_search/compat.py:60: UserWarning: ddg_images is deprecated. Use DDGS().images() generatorwarnings.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 deprecatedwarnings.warn(parameter page is deprecated)
/opt/conda/lib/python3.7/site-packages/duckduckgo_search/compat.py:66: UserWarning: parameter max_results is deprecatedwarnings.warn(parameter max_results is deprecated)
---------------------------------------------------------------------------
HTTPStatusError Traceback (most recent call last)
/tmp/ipykernel_18/2432147335.py in module1 #NB: search_images depends on duckduckgo.com, which doesnt 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_images1)4 urls[0]/tmp/ipykernel_18/1717929076.py in search_images(term, max_images)4 def search_images(term, max_images30):5 print(fSearching for {term})
---- 6 return L(ddg_images(term, max_resultsmax_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_imagetype_image,81 layoutlayout,
--- 82 license_imagelicense_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, paramspayload)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 ex90 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_typeerror_type)
-- 749 raise HTTPStatusError(message, requestrequest, responseself)750 751 def json(self, **kwargs: typing.Any) - typing.Any:HTTPStatusError: Client error 403 Forbidden for url https://duckduckgo.com/i.js?lwt-wtojsons0qbird%20photosvqd4-120319640991756807142594628538299407667f%2C%2C%2C%2C%2Cp1
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_images2 from fastcore.all import *3 4 def search_images(term, max_images30):5 print(fSearching 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 fastbookfrom fastbook import search_images_ddg# urls search_images(bird photos, max_images1)
urls search_images_ddg(bird photos, max_images1)
urls[0]
好了大功告成了还有问题的话可以参考一下我的note
Hello World Hello bird - Is it a bird | Kaggle