deedy5 / duckduckgo_search

Search for words, documents, images, videos, news, maps and text translation using the DuckDuckGo.com search engine. Downloading files and images to a local hard drive.
MIT License
948 stars 117 forks source link

JSONDecodeError can be thrown when using ddg_news on queries that have no results #9

Closed therhappy closed 1 year ago

therhappy commented 1 year ago

Hello,

Thanks a lot for putting up this library! I've started using it and so far it's been a great help.

Issue

I recently tried using ddg_news and most of the time it works fine. However, when trying for elements which have possibly 0 answers, this error is thrown:

'from duckduckgo_search import ddg_news items = ddg_news(keywords="Jeremy Belpois", max_results=3, safesearch='Off', region='wt-wt') --------------------------------------------------------------------------- JSONDecodeError Traceback (most recent call last) c:\Anaconda3\lib\site-packages\requests\models.py in json(self, kwargs) 959 try: --> 960 return complexjson.loads(self.content.decode(encoding), kwargs) 961 except UnicodeDecodeError:

c:\Anaconda3\lib\json__init__.py in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw) 345 parse_constant is None and object_pairs_hook is None and not kw): --> 346 return _default_decoder.decode(s) 347 if cls is None:

c:\Anaconda3\lib\json\decoder.py in decode(self, s, _w) 336 """ --> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 338 end = _w(s, end).end()

c:\Anaconda3\lib\json\decoder.py in raw_decode(self, s, idx) 354 except StopIteration as err: --> 355 raise JSONDecodeError("Expecting value", s, err.value) from None 356 return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred: ... --> 968 raise RequestsJSONDecodeError(e.msg, e.doc, e.pos) 969 970 try:

JSONDecodeError: Expecting value: line 1 column 1 (char 0)`

Versions

I'm using python 3.9, requests==2.28.1, duckduckgo-search==1.8.

Thoughts

It is likely that this is because a query is passed empty to a parser. However when this error happens, the execution time is significantly longer (about 9s), which means it is a bit hard to plan around without losing quite some time in the program execution.

I'll continue diving on this on my end, unless you have a quick fix in mind.