Skip to content

Commit

Permalink
Merge pull request #6 from deedy5/5-max_results-parameter-is-misleading
Browse files Browse the repository at this point in the history
v1.5.2
  • Loading branch information
deedy5 authored Apr 14, 2022
2 parents 5c8de02 + 86ac7c0 commit 862f059
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions duckduckgo_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from lxml import html


__version__ = '1.5.1'
__version__ = '1.5.2'


session = requests.Session()
Expand Down Expand Up @@ -166,6 +166,7 @@ def ddg(keywords, region='wt-wt', safesearch='Moderate', time=None, max_results=
payload = {n: v for n, v in zip(names, values)}
sleep(2)
'''
results = results[:max_results]
if save_csv:
keywords = keywords.replace('"',"'")
_save_csv(f"{keywords}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv", results)
Expand Down Expand Up @@ -224,11 +225,12 @@ def ddg_images(keywords, region='wt-wt', safesearch='Moderate', time=None, size=
}

results = []
while payload['s'] < max_results:
while payload['s'] < max_results or len(results) < max_results:
res = session.get("https://duckduckgo.com/i.js", params=payload)
data = res.json()
results.extend(r for r in data['results'])
payload['s'] += 100
results = results[:max_results]

if save_csv:
keywords = keywords.replace('"',"'")
Expand Down Expand Up @@ -290,7 +292,7 @@ def ddg_news(keywords, region='wt-wt', safesearch='Moderate', time=None, max_res
}
data_previous, cache = [], set()
results = []
while params['s'] < min(max_results, 240):
while params['s'] < min(max_results, 240) or len(results) < max_results:
resp = session.get('https://duckduckgo.com/news.js', params=params)
data = resp.json()['results']
if data_previous and data == data_previous:
Expand All @@ -313,6 +315,7 @@ def ddg_news(keywords, region='wt-wt', safesearch='Moderate', time=None, max_res
})
params['s'] += 30
sleep(0.2)
results = results[:max_results]
results = sorted(results, key=lambda x: x['date'], reverse=True)
if save_csv:
keywords = keywords.replace('"',"'")
Expand Down

0 comments on commit 862f059

Please sign in to comment.