@@ -1,24 +1,48 @@
1+## DuckDuckGo (Web)
2+#
3+# @website https://duckduckgo.com/
4+# @provide-api yes (https://duckduckgo.com/api), but not all results from search-site
5+#
6+# @using-api no
7+# @results HTML (using search portal)
8+# @stable no (HTML can change)
9+# @parse url, title, content
10+#
11+# @todo rewrite to api
12+# @todo language support
13+
114from urllib import urlencode
215from lxml.html import fromstring
316from searx.utils import html_to_text
417
5−url = 'https://duckduckgo.com/html?{query}&s={offset}'
18+# engine dependent config
19+categories = ['general']
20+paging = True
621locale = 'us-en'
722
23+# search-url
24+url = 'https://duckduckgo.com/html?{query}&s={offset}'
25+
26+# specific xpath variables
27+result_xpath = '//div[@class="results_links results_links_deep web-result"]' # noqa
28+url_xpath = './/a[@class="large"]/@href'
29+title_xpath = './/a[@class="large"]//text()'
30+content_xpath = './/div[@class="snippet"]//text()'
831
32+
33+# do search-request
934def request(query, params):
1035 offset = (params['pageno'] - 1) * 30
11− q = urlencode({'q': query,
12− 'l': locale})
13− params['url'] = url.format(query=q, offset=offset)
36+
37+ params['url'] = url.format(
38+ query=urlencode({'q': query, 'l': locale}),
39+ offset=offset)
40+
1441 return params
1542
1643
44+# get response from search-request
1745def response(resp):
18− result_xpath = '//div[@class="results_links results_links_deep web-result"]' # noqa
19− url_xpath = './/a[@class="large"]/@href'
20− title_xpath = './/a[@class="large"]//text()'
21− content_xpath = './/div[@class="snippet"]//text()'
2246 results = []
2347
2448 doc = fromstring(resp.text)
@@ -28,38 +52,17 @@ def response(resp):
2852 res_url = r.xpath(url_xpath)[-1]
2953 except:
3054 continue
55+
3156 if not res_url:
3257 continue
58+
3359 title = html_to_text(''.join(r.xpath(title_xpath)))
3460 content = html_to_text(''.join(r.xpath(content_xpath)))
61+
62+ # append result
3563 results.append({'title': title,
3664 'content': content,
3765 'url': res_url})
3866
67+ # return results
3968 return results
40−
41−
42−#from json import loads
43−#search_url = url + 'd.js?{query}&p=1&s={offset}'
44−#
45−#paging = True
46−#
47−#
48−#def request(query, params):
49−# offset = (params['pageno'] - 1) * 30
50−# q = urlencode({'q': query,
51−# 'l': locale})
52−# params['url'] = search_url.format(query=q, offset=offset)
53−# return params
54−#
55−#
56−#def response(resp):
57−# results = []
58−# search_res = loads(resp.text[resp.text.find('[{'):-2])[:-1]
59−# for r in search_res:
60−# if not r.get('t'):
61−# continue
62−# results.append({'title': r['t'],
63−# 'content': html_to_text(r['a']),
64−# 'url': r['u']})
65−# return results